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

3

28

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?

Constituent answered 27/4, 2012 at 22:27 Comment(2)
The short answer is you can't (or at least you shouldn't). The columns of data frames must all be atomic vectors (i.e. the same data type), and you're trying to mix numeric (DF1) and character/factor (DF2).Behlke
It will "work" if the columns of DF2 are characters rather than factors. But you still probably should find a different way to link these data sets.Behlke
P
16

Here's what you should do:

DFtranspose <- cbind(t(DF1[2, ]), t(DF2))
rownames(DFtranspose) <- DF1[1, ]
Photostat answered 28/4, 2012 at 12:7 Comment(0)
M
16

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

bothdfs <- rbind(df1, df2)

Column names will automatically be preserved.

Mercaptide answered 18/12, 2019 at 16:55 Comment(0)
Z
3

I agree with the comments, that this is probably a bad idea, but here's how you could do it.

First of all, with the list-based data.frame isn't going to combine too well in this fashion. If you want to rbind the data, you'll be better off converting them to a matrix. Note that you're going to have to choose a single type for each row of your data frame, so you can't keep your numbers as numbers if they're bound to characters. If you're willing to treat everything as a character, give this a go:

> df1 <- data.frame(pt1="a", pt2="b", row.names=1)
> rownames(df1) <- "e"
> df2 <- data.frame(letters[1:4], pt1=1:4, pt2=2:5, row.names=1)
> rbind(as.matrix(df2), as.matrix(df1))
  pt1 pt2
a "1" "2"
b "2" "3"
c "3" "4"
d "4" "5"
e "a" "b"
Zondra answered 27/4, 2012 at 22:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.