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

Here's what you should do:

DFtranspose <- cbind(t(DF1[2, ]), t(DF2))
rownames(DFtranspose) <- DF1[1, ]

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"

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

bothdfs <- rbind(df1, df2)

Column names will automatically be preserved.

Tags:

R

Dataframe

Rows