【问题标题】:Matching multiple data in R taking into account the control variable考虑到控制变量,匹配 R 中的多个数据
【发布时间】:2020-10-16 11:06:40
【问题描述】:

这里是我的数据

mydatatr=structure(list(channel_id = c(1755L, 1755L), multifr_type = c(0L, 
0L), offer_category_id = c(718L, 718L), adapter_id = c(3L, 3L
), adapter_id2 = c(0L, 0L), airline1 = c(238L, 238L), airline2 = c(0L, 
0L), meta_ui_type = c(0L, 0L), offer_flight_type_category_id = c(1L, 
1L), discount_category_id = c(1L, 6L), flight_area = c(1L, 1L
), count_sessions = c(13297L, 12026L), count_orders = c(3264L, 
2400L), conversion = c(0.245, 0.2)), class = "data.frame", row.names = c(NA, 
-2L))

第二个数据集

mydatincom=structure(list(channel_id = c(1755L, 1755L, 1755L), multifr_type = c(0L, 
0L, 0L), offer_category_id = c(718L, 718L, 14L), adapter_id = c(3L, 
3L, 3L), adapter_id2 = c(0L, 0L, 0L), airline1 = c(238L, 238L, 
13L), airline2 = c(0L, 0L, 0L), meta_ui_type = c(0L, 0L, 0L), 
    offer_flight_type_category_id = c(1L, 1L, 1L), discount_category_id = c(1L, 
    6L, 1L), flight_area = c(1L, 1L, 2L), count_sessions = c(13297L, 
    12026L, 0L), count_orders = c(3264L, 2400L, 0L), conversion = c(0.245, 
    0.2, 0)), class = "data.frame", row.names = c(NA, -3L))

这里控制数据集

    control_var=structure(list(offer_category_id = c(10L, 18L, 48L, 49L, 50L, 
51L, 52L, 64L, 65L, 67L)), class = "data.frame", row.names = c(NA, 
-10L))

最后一个数据集

aggr=structure(list(channel_id = 1755L, multifr_type = 0L, adapter_id = 3L, 
    adapter_id2 = 0L, airline1 = 13L, airline2 = 0L, meta_ui_type = 0L, 
    offer_flight_type_category_id = 1L, discount_category_id = 1L, 
    flight_area = 2L, count_sessions = 562L, count_orders = 22L, 
    conversion = structure(1L, .Label = "0.039", class = "factor")), class = "data.frame", row.names = c(NA, 
-1L))

我需要通过这些变量channel_id+multifr_type+adapter_id+adapter_id2+airline1+airline2+meta_ui_type+offer_flight_type_category_id+discount_category_id+flight_area 加入mydatatrmydaticnom,但条件是:

如果在mydatincom中加入这些数据集时,有offer_category_id的值,但control_var数据中没有这个值, 然后我们在aggr 数据集中寻找变量channel_id + multifr_type + adapter_id + adapter_id2 + airline1 + airline2 + meta_ui_type + offer_flight_type_category_id + discount_category_id + flight_area,如果这些值在aggr 中匹配,我们加入aggrmydatincom 并将字段count_sessions + count_orders+conversion 放入来自aggr 的新数据中.

为了更清楚,让我们看一个具体的例子。 例如,在mydatincom 中有offer_category_id 不在control_var(value=14) 中, 但加入 are channel_id + multifr_type + adapter_id + adapter_id2 + airline1 + airline2 + meta_ui_type + offer_flight_type_category_id + discount_category_id + flight 的变量 在mydatincom 中具有相同的值,在aggr 中有。所以取自aggrcount_sessions + count_orders + conversion。

count_sessions count_orders conversion
562                22           0.039

并将其放入最终数据集中,并通过 put other 替换 14 值 所以结果会是这样的

desired_result=structure(list(channel_id = c(1755L, 1755L, 1755L), multifr_type = c(0L, 
0L, 0L), offer_category_id = structure(c(1L, 1L, 2L), .Label = c("718", 
"other"), class = "factor"), adapter_id = c(3L, 3L, 3L), adapter_id2 = c(0L, 
0L, 0L), airline1 = c(238L, 238L, 13L), airline2 = c(0L, 0L, 
0L), meta_ui_type = c(0L, 0L, 0L), offer_flight_type_category_id = c(1L, 
1L, 1L), discount_category_id = c(1L, 6L, 1L), flight_area = c(1L, 
1L, 2L), count_sessions = c(13297L, 12026L, 562L), count_orders = c(3264L, 
2400L, 22L), conversion = structure(c(2L, 1L, 3L), .Label = c("0,2", 
"0,245", "0.039"), class = "factor")), class = "data.frame", row.names = c(NA, 
-3L))

  channel_id multifr_type offer_category_id adapter_id adapter_id2 airline1 airline2 meta_ui_type
1       1755            0               718          3           0      238        0            0
2       1755            0               718          3           0      238        0            0
3       1755            0             other          3           0       13        0            0
  offer_flight_type_category_id discount_category_id flight_area count_sessions count_orders
1                             1                    1           1          13297         3264
2                             1                    6           1          12026         2400
3                             1                    1           2            562           22
  conversion
1      0,245
2        0,2
3      0.039

这样的匹配怎么做?

【问题讨论】:

  • offer_category_id 中的值 718 也不在 control_var 中,对吗?所以你实际想要的输出应该已经对第一行和第二行应用了额外的步骤,还是我弄错了?另外,让我说一下我从你的文字中得到了什么,你说它是否正确:如果offer_category_idcontrol_var 中,我们只是rbind(mydatatr, mydatincom),否则我们rbind(mydatatr, aggr) - 因为只有最后三列发生变化,即你想要什么 - 并将 offer_category_id 更改为 "other"
  • @RicardoSemiãoeCastro,718 怎么样,它是正确的,在 mydatatr 中引起。还是我弄错了?你说的对。你确实理解我的文字!
  • 我需要分析mydatincom的每一行,第一行有718对应offer_category_idcontrol_var 中不存在此数字,因此这是进行此转换的原因。但是,你的意思是718 存在于mydatatr 中已经存在的值中,我不进行转换,对吗?或者我根本不将该行放入新数据框中?

标签: r dplyr data.table


【解决方案1】:

这基本上是我评论的内容,但忽略了mydatatr 中已经存在的案例。因为我们忽略了来自mydatatr 的行,但我们不想对aggr 做同样的事情——循环索引i 不断增加——我放置了索引i-k,只有当一个使用来自aggr 的行。

k=0
for(i in 1:nrow(mydatincom)){
  
  if(! mydatincom$offer_category_id[i] %in% mydatatr$offer_category_id){
  #If the value isn't already in mydatatr
    
    if(! mydatincom$offer_category_id[i] %in% control_var){
      #If the value isn't in the control var
      newrow = tibble::add_column(aggr[i-k,], "other", .after="multifr_type")
      colnames(newrow) = colnames(mydatatr)
      mydatatr = rbind(mydatatr, newrow)}
    
    else{
      mydatatr = rbind(mydatatr, mydatincom[i,])}}
  else{k=k+1}} #Count the numbers of ignored cases from the 1st "if"

【讨论】:

    猜你喜欢
    • 2013-09-08
    • 1970-01-01
    • 2022-12-11
    • 2021-08-15
    • 2020-11-06
    • 2017-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多