R 如何将两个列名称稍有不同的数据集一个接一个地追加?
数据集1:R 如何将两个列名称稍有不同的数据集一个接一个地追加?,r,R,数据集1: ID Name Territory Sales 1 Richard NY 59 8 Sam California 44 数据集2: Terr ID Name Comments LA 5 Rick yes MH 11 Oly no 我希望最终数据集只包含第一个数据集的列,并且标识区域与Terr相同,并且不提出注释列 最终数据应如下所示: ID Name Territory Sal
ID Name Territory Sales
1 Richard NY 59
8 Sam California 44
数据集2:
Terr ID Name Comments
LA 5 Rick yes
MH 11 Oly no
我希望最终数据集只包含第一个数据集的列,并且标识区域
与Terr
相同,并且不提出注释
列
最终数据应如下所示:
ID Name Territory Sales
1 Richard NY 59
8 Sam California 44
5 Rick LA NA
11 Oly MH NA
提前感谢一个可能的解决方案:
# create a named vector with names from 'set2'
# with the positions of the matching columns in 'set1'
nms2 <- sort(unlist(sapply(names(set2), agrep, x = names(set1))))
# only keep the columns in 'set2' for which a match is found
# and give them the same names as in 'set1'
set2 <- setNames(set2[names(nms2)], names(set1[nms2]))
# bind the two dataset together
# option 1:
library(dplyr)
bind_rows(set1, set2)
# option 2:
library(data.table)
rbindlist(list(set1, set2), fill = TRUE)
使用数据:
set1 <- structure(list(ID = c(1L, 8L),
Name = c("Richard", "Sam"),
Territory = c("NY", "California"),
Sales = c(59L, 44L)),
.Names = c("ID", "Name", "Territory", "Sales"), class = "data.frame", row.names = c(NA, -2L))
set2 <- structure(list(Terr = c("LA", "MH"),
ID = c(5L, 11L),
Name = c("Rick", "Oly"),
Comments = c("yes", "no")),
.Names = c("Terr", "ID", "Name", "Comments"), class = "data.frame", row.names = c(NA, -2L))
set1rbind(set1,setNames(set2[,-4],names(set1))
或库(data.table);rbindlist(list(set1,set2[,-4]))
My列的顺序也不一样。我刚刚编辑了问题,请现在回答。谢谢rbind(set1,setNames(set2[,c(2,3,1)],names(set1))
请再次检查问题。。我刚换了。因为我的列顺序不一样
set1 <- structure(list(ID = c(1L, 8L),
Name = c("Richard", "Sam"),
Territory = c("NY", "California"),
Sales = c(59L, 44L)),
.Names = c("ID", "Name", "Territory", "Sales"), class = "data.frame", row.names = c(NA, -2L))
set2 <- structure(list(Terr = c("LA", "MH"),
ID = c(5L, 11L),
Name = c("Rick", "Oly"),
Comments = c("yes", "no")),
.Names = c("Terr", "ID", "Name", "Comments"), class = "data.frame", row.names = c(NA, -2L))