0

I'm trying to figure out how to append more data to an existing dataframe one column at a time. For example: I have this dataframe:

df <- data.frame("x" = c(1, 2, 3, 4, 5), "y" = c(0.255, 0.236, 0.587, 0.369, 0.789))

Here are additional data I want to append. I want to add x2 to the bottom of the x column and then add y2 to the bottom of the y column.

x2 <- c(6, 7, 8, 9, 10)
y2 <- c(0.236, 0.963, 0.356, 0.489, 0.333)

This is what I want the dataframe to look like after.

x      y
1  0.255
2  0.236
3  0.587
4  0.369
5  0.789
6  0.236
7  0.963
8  0.356
9  0.489
10 0.333
Myco
  • 101
  • 2
  • 10
  • or this one https://stackoverflow.com/questions/10358680/adding-a-one-dataframe-to-the-the-end-of-another-data-frame-in-r or this one https://stackoverflow.com/questions/42229019/how-to-append-smaller-dataframe-to-another-dataframe-in-r or this one https://stackoverflow.com/questions/29402528/append-data-frames-together-in-a-for-loop or this one https://stackoverflow.com/questions/8169323/r-concatenate-two-dataframes or this one https://stackoverflow.com/questions/3402371/combine-two-data-frames-by-rows-rbind-when-they-have-different-sets-of-columns – M-- Nov 19 '18 at 22:30

2 Answers2

1

You will need to make sure that the column names are same when appending data.

In your case:

df2 <- data.frame(x2, y2)   #creating a dataframe
names(df2) <- names(df)     #changing the column header names as this is a requirement for append

df <- rbind(df, df2)    #appending
SmitM
  • 1,366
  • 1
  • 8
  • 14
0

This should do the trick, unless I am missing something:

df <- data.frame("x" = c(1, 2, 3, 4, 5), "y" = c(0.255, 0.236, 0.587, 0.369, 0.789))

Change the names of the data to be added to have the same name as the original dataframe

x <- c(6, 7, 8, 9, 10)
y <- c(0.236, 0.963, 0.356, 0.489, 0.333)
df2 <- bind_cols(list(x = x, y = y))
df3 <- bind_rows(list(df, df2))
André.B
  • 617
  • 8
  • 17
  • The above code can be fed multiple df/tibbles simultaneously without the use of a do.call loop as well. As below the names need to be constant between dataframes for them to line up. – André.B Nov 19 '18 at 22:36