将具有相同列名的两个数据帧组合在一起

时间:2020-12-05 22:51:47

I have two data.frame with this format (this is small part of the data sets):

我有两个带有这种格式的data.frame(这是数据集的一小部分):

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:

我想将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

1 个解决方案

#1


18  

You can use rbind:

你可以使用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)

#merge by row
df3 <- rbind(df1,df2)

#update IDs
df3$ID <- 1:nrow(df3)

#output
df3

#1


18  

You can use rbind:

你可以使用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)

#merge by row
df3 <- rbind(df1,df2)

#update IDs
df3$ID <- 1:nrow(df3)

#output
df3