Это может быть не идеальный ответ, но здесь есть опция без слияния, без объединения с использованием sapply
, поскольку мы хотим объединить два кадра данных, используя только один столбец
#Name the cols which you want in the final data frame
cols <- c("Id", "Name", "Age", "Sex","Grade")
#Get all unique id's
ids <- union(d1$Id, d2$Id)
#Loop over each ID
data.frame(t(sapply(ids, function(x) {
#Get indices in d1 where Id is present
d1inds <- d1$Id == x
#Get indices in d2 where Id is present
d2inds <- d2$Id == x
#If the Id is present in both d1 AND d2
if (any(d1inds) & any(d2inds))
#Combine d2 and d1 and select only cols column
#This is based on your expected output that in case if the ID is same
#we want to prefer Name and Age column from d2 rather than d1
return(cbind(d2[d2inds, ], d1[d1inds, ])[cols])
#If you want to prefer d1 over d2, we can do
#return(cbind(d1[d1inds, ], d2[d2inds, ])[cols])
#If the Id is present only in d1, add a "Sex" column with NA
if (any(d1inds))
return(cbind(d1[d1inds, ], "Sex" = NA)[cols])
#If the Id is present only in d2, add a "Grade" column with NA
else
return(cbind(d2[d2inds, ], "Grade" = NA)[cols])
})))
# Id Name Age Sex Grade
#1 1 Yann 28 M 15
#2 2 Anne 19 NA 12
#3 3 Sabri 21 M 18
#4 4 Jui 15 F NA
данные
d1 <- data.frame(Id=1:3,Name=c("Yann","Anne","Sabri"),Age=c(21,19,31),
Height=c(178,169,192),Grade=c(15,12,18), stringsAsFactors = FALSE)
d2 <- data.frame(Id=c(1,3,4),Name=c("Yann","Sabri","Jui"),Age=c(28,21,15),
Sex=c("M","M","F"),City=c("Paris","Paris","Toulouse"), stringsAsFactors = FALSE)