Reputation: 161
I need to replace a value in df1 with a value in df2 based on two variables in df1.
The 'linking variable' in df1 is called VacancyId
and in df2 reference
. In df1 the observations have multiple rows and I only need to select one of them. This row that needs to have a replacement, is indicated by the factorlevel which has (Recruitz)
in its name in the variable VacancyBankName
. The posibilities are:
df1 = d.9weeks
df2 = recruitz
Some data to make more sense:
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
So if:
recruitz$reference == d.9weeks$VacancyId
AND d.9weeks$VacancyBankName == '... (Recruitz)'
THEN d.9weeks$ViewsByVacancyBankAndVacancyCount <- recruitz$clicks
How can I do this in R?
Upvotes: 1
Views: 73
Reputation: 4482
I think the best way is to merge the 2 df
together first, and then do an ifelse
statement using data.table
. So
library(data.table)
df_final <- as.data.table(merge(df1,df2[,.(VacancyId=reference,clicks)],by="VacancyId"))
df_final[,ViewsByVacancyBankAndVacancyCount:=ifelse(VacancyBankName==...)]
Upvotes: 1