根据R中的两个变量和两个数据集替换值

时间:2017-07-27 10:46:54

标签: r dataset

我需要根据df1中的两个变量将df1中的值替换为df2中的值。

'链接变量'在df1中称为VacancyId,在df2 reference中。在df1中,观察有多行,我只需要选择其中一行。需要替换的此行由因子级别指示,其在变量(Recruitz)中的名称中包含VacancyBankName。可能性是:

  • Google Recruitment(Recruitz)
  • 社交媒体Campagne(Recruitz)
  • Instagram Campagne(Recruitz)
  • 社交媒体Campagne +(Recruitz)
  • Facebook& Twitter Campagne(Recruitz)
  • Google Recruitment(Recruitz)
  • Google Recruitment Plus(Recruitz)
  • Facebook - Adwords campagne(Recruitz)

df1 = d.9weeks

df2 = recruitz

有些数据更有意义:

head(d.9weeks[,c(1,12,16)],15)
VacancyId                 VacancyBankName  ViewsByVacancyBankAndVacancyCount
57820                     Monsterboard                                31
57820                     Facebook - Adwords campagne (Recruitz)      387
57822                     Monsterboard                                1
57871                     Monsterboard                                228
57818                     LinkedIn (Jobportal)                        0
57822                     Stepstone                                   3
57822                     LinkedIn (Jobportal)                        1
57871                     LinkedIn (Jobportal)                        2
57818                     Monsterboard                                76
57820                     ICTerGezocht                                0
57871                     Social Media Campagne (Recruitz)            376
57871                     Stepstone                                   56
57820                     Stepstone                                   92
57820                     LinkedIn (Jobportal)                        2
57775                     Intermediair Premium                        9
57775                     LinkedIn (Jobportal)                        0


head(recruitz[,c(2,3)], 20)
reference clicks
57871     326
57820     75
73823    105
73826    114
73785     99
73857     30
73845    177
73944     64
73851      6
73941    114
73902    132
73959    115
73946    189
73962     74
73979     93
73947    152
74006    134
73982    207
74033     60
74022     97

所以如果: recruitz$reference == d.9weeks$VacancyIdd.9weeks$VacancyBankName == '... (Recruitz)'

然后d.9weeks$ViewsByVacancyBankAndVacancyCount <- recruitz$clicks

我怎样才能在R?

中这样做

1 个答案:

答案 0 :(得分:1)

我认为最好的方法是首先将2 df合并在一起,然后使用ifelse执行data.table语句。所以

library(data.table)

df_final <- as.data.table(merge(df1,df2[,.(VacancyId=reference,clicks)],by="VacancyId")) df_final[,ViewsByVacancyBankAndVacancyCount:=ifelse(VacancyBankName==...)]