Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a one dataframe to the end of another data.frame in R

Tags:

I'm having difficulty adding one to the bottom of another data frame.

I have one data frame (let's call it DF1) that has 1 row and 5 columns. I have another dataframe (lets call it DF2) that has 50 rows, and 5 columns. I set it up such that the columns between BOTH data frames match - they have the same columns. In fact, DF1 is a calculation based on DF2.

This is what DF1 looks like:

   row.names     pt1     pt2     pt3     pt4    calc          0.93    0.45    0.28    0.54 

This is what DF2 looks like:

   row.names     pt1     pt2     pt3     pt4    SNP1          AA      AG      AG      AA           SNP2          CT      CT      TC      CC    SNP3          GG      CG      CG     <NA>    SNP4          AA      GG      AG      AA    SNP5         <NA>    <NA>    <NA>    <NA> 

DF1 is supposed to be the the number of actual data points(# of values that is not missing) divided by the total number of possible values.

SO.. I want to add DF1 to the bottom of DF2 to look like this:

   row.names     pt1     pt2     pt3     pt4    SNP1          AA      AG      AG      AA           SNP2          CT      CT      TC      CC    SNP3          GG      CG      CG     <NA>    SNP4          AA      GG      AG      AA    SNP5         <NA>    <NA>    <NA>    <NA>    calc          0.93    0.45    0.28    0.54 

When I tried using

 both.dfs <- rbind(DF1, DF2)  # DF1 is first here 

DF1 is the first row in DF2. I NEED it to be the LAST row.

When I tried using

both.dfs <- rbind(DF2, DF1)  # DF2 is first here 

I get an error:

Warning messages: 1: In `[<-.factor`(`*tmp*`, iseq, value = 0.84) :   invalid factor level, NAs generated 2: In `[<-.factor`(`*tmp*`, iseq, value = 0.84) :   invalid factor level, NAs generated 3: In `[<-.factor`(`*tmp*`, iseq, value = 0.84) :   invalid factor level, NAs generated 4: In `[<-.factor`(`*tmp*`, iseq, value = 0.74) :   invalid factor level, NAs generated 

I've tried merge, I've tried adding a new row to DF2 and then subbing in the values of DF2..nothing seems to work! I'm in desperate need of some help! Anyone?

like image 312
Sheila Avatar asked Apr 27 '12 22:04

Sheila


People also ask

How do you add a DataFrame to the end of another DataFrame in R?

Appending a Column to data frame To append a column to the data frame in R, use the symbol $ to append the data frame variable and add a column. Then, create a new vector that acts as a column and add that vector to the existing data frame in a column.

How do I append a DataFrame in another column in R?

Method 1 : Using plyr package rbind. fill() method in R is an enhancement of the rbind() method in base R, is used to combine data frames with different columns. The column names are number may be different in the input data frames. Missing columns of the corresponding data frames are filled with NA.

How do I append two data frames in R?

Method 1: Use rbind() to Append Data Frames. This first method assumes that you have two data frames with the same column names. By using the rbind() function, we can easily append the rows of the second data frame to the end of the first data frame. For example:

How to add a column to a Dataframe in R?

Since a DataFrame in R is a list of vectors where each vector represents an individual column of that DataFrame, we can add a column to a DataFrame just by adding the corresponding new vector to this "list". The syntax is as follows:

How do I append two data frames in SQL Server?

Method 1: Use rbind () to Append Data Frames. This first method assumes that you have two data frames with the same column names. By using the rbind () function, we can easily append the rows of the second data frame to the end of the first data frame.

How to combine two data frames with the same column names?

This first method assumes that you have two data frames with the same column names. By using the rbind () function, we can easily append the rows of the second data frame to the end of the first data frame.


2 Answers

Here's what you should do:

DFtranspose <- cbind(t(DF1[2, ]), t(DF2)) rownames(DFtranspose) <- DF1[1, ] 
like image 179
IRTFM Avatar answered Oct 01 '22 08:10

IRTFM


As long as you are dealing wiht dataframes, you can use rbind():

bothdfs <- rbind(df1, df2) 

Column names will automatically be preserved.

like image 22
Swift Arrow Avatar answered Oct 01 '22 07:10

Swift Arrow