我有两个具有这种格式的 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
数据框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
我想将 data.frame2 添加到 data.frame1 的末尾,所以最后我有这样的内容:
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
我们可以用rbind http://stat.ethz.ch/R-manual/R-devel/library/base/html/cbind.html:
cbind, rbind:采用向量、矩阵或数据框参数序列,并分别按列或行组合。
# 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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)