这是两个数据框。如何使用第二个的productid合并两个数据框,但是第一个数据框包含更多行,并且其中一些行必须在文本列NA中
"addresses": [{
"firstName" : "firstName",
"lastName": "lastName",
"street": "street",
"city": "city",
"postcode": 123,
"countryCode": "code"
}]
预期输出示例
dframe1 = data.frame(sample = c("text1", "text2", "text3", "text4", "text5", "text6", "text7", "text8", "text9", "text10"),
productid = c(33, 5, 33, 34, 12, 54, 22, 9, 45, 22),
manifucturerid = c(1, 1, 2, 2, 3, 4, 5, 6, 7, 7))
dframe2 = data.frame(productid = c(33, 33, 34, 54, 22, 45, 22),
text = c("a,b", "a,b", "c,d", "e,f,g", "h,i,j,k", "l,m", "h,i,j,k"))
答案 0 :(得分:0)
您可以按productid使用dplyr::left_join()
。左联接保留dframe1的所有行,并在产品ID匹配时将文本添加到新列。
library(dplyr)
dframe <- left_join(dframe1, dframe2, by = "productid")
# sample productid manifucturerid text
# 1 text1 33 1 a,b
# 2 text2 5 1 NA
# 3 text3 33 2 a,b
# 4 text4 34 2 c,d
# 5 text5 12 3 NA
# 6 text6 54 4 e,f,g
# 7 text7 22 5 h,i,j,k
# 8 text8 9 6 NA
# 9 text9 45 7 l,m
# 10 text10 22 7 h,i,j,k