匹配来自2个数据帧的列,并根据匹配在第二个数据帧中创建新列

时间:2018-03-29 13:04:09

标签: r dataframe match

我有两个数据框。他们的记录数量不均。

DF1:

structure(list(subscriberid = c(1085369583, 1244038459, 1240958486, 
1088671175), cweAge = c(32, 30, 49, 30), cweGen = c("m", "m", 
"m", "m"), hwAge = c(60, 30, 47, 30), hwGen = c("f", "f", "f", 
"f"), cwe = c(1, 1, 1, 1), hw = c(1, 1, 1, 1)), .Names = c("subscriberid", 
"cweAge", "cweGen", "hwAge", "hwGen", "cwe", "hw"), row.names = 453:456, class = "data.frame")

DF2

structure(list(subscriberid = c(1244038459, 1240958486, 1234200861
), mem_1_gen = c("f", "f", "m"), mem_1_age = c(32, 23, 12), mem_2_gen = c("m", 
"m", "f"), mem_2_age = c(38, 48, 12), mem_3_gen = c("0", "f", 
"f")), .Names = c("subscriberid", "mem_1_gen", "mem_1_age", "mem_2_gen", 
"mem_2_age", "mem_3_gen"), row.names = c(1L, 3L, 9L), class = "data.frame")

它们看起来像这样: 的 DF1

    subscriberid cweAge cweGen hwAge hwGen cwe hw
453   1085369583     32      m    60     f   1  1
454   1244038459     30      m    30     f   1  1
455   1240958486     49      m    47     f   1  1
456   1088671175     30      m    30     f   1  1

DF2

  subscriberid mem_1_gen mem_1_age mem_2_gen mem_2_age mem_3_gen
1   1244038459         f        32         m        38         0
3   1240958486         f        23         m        48         f
9   1234200861         m        12         f        12         f

我想在mem_1_cwe中添加7列DF2DF1$cwesubscriberid中匹配的AGE数据和GENDER

我尝试过与互动匹配:

df2$mem_1_CWE <- df1$cweGen[match(interaction(df2$subscriberid, df1$subscriberid),interaction(df2$mem_1_age, df1$cweAge))]

但由于每个数据框中的记录数量不同,我收到以下错误:

Error in `$<-.data.frame`(`*tmp*`, mem_1_CWE, value = c(NA_character_,  : 
  replacement has 1918 rows, data has 1915
In addition: Warning messages:
1: In ans * length(l) + if1 :
  longer object length is not a multiple of shorter object length
2: In ans * length(l) + if1 :
  longer object length is not a multiple of shorter object length

0 个答案:

没有答案