I have two data.frame with this format (this is small part of the data sets):
data.frame 1
ID precip lat lon
1 45 115 -122.5
2 42.5 130 -122.5
3 40 155 -122.5
4 37.5 140 -122.5
data.frame 2
precip lat lon
1 108.61 115 -122.5
2 85.83 130 -122.5
3 81.01 155 -122.5
4 NA 140 -122.5
I want to add data.frame2 to the end of data.frame1 so at the end I have something like this:
ID precip lat lon
1 45 115 -122.5
2 42.5 130 -122.5
3 40 155 -122.5
4 37.5 140 -122.5
5 108.61 115 -122.5
6 85.83 130 -122.5
7 81.01 155 -122.5
8 NA 140 -122.5
We can use rbind:
cbind, rbind: Take a sequence of vector, matrix or data-frame arguments and combine by columns or rows, respectively.
# dataframe 1
df1 <- read.table(text = "
ID precip lat lon
1 45 115 -122.5
2 42.5 130 -122.5
3 40 155 -122.5
4 37.5 140 -122.5
", header = TRUE)
# dataframe 2
df2 <- read.table(text = "
ID precip lat lon
1 108.61 115 -122.5
2 85.83 130 -122.5
3 81.01 155 -122.5
4 NA 140 -122.5
", header = TRUE)
# combine by row
df3 <- rbind(df1, df2)
# update ID column
df3$ID <- 1:nrow(df3)
# output
df3
# ID precip lat lon
# 1 1 45.00 115 -122.5
# 2 2 42.50 130 -122.5
# 3 3 40.00 155 -122.5
# 4 4 37.50 140 -122.5
# 5 5 108.61 115 -122.5
# 6 6 85.83 130 -122.5
# 7 7 81.01 155 -122.5
# 8 8 NA 140 -122.5
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With