jeudi 1 décembre 2016

replacing a value in column X based on columns Y with R

i've gone through several answers and tried the following but each either yields an error or an un-wanted result:

here's the data:

Network                 Campaign
Moburst_Chartboost      Test Campaign
Moburst_Chartboost      Test Campaign 
Moburst_Appnext         unknown
Moburst_Appnext         1065

i'd like to replace "Test Campaign" with "1055" whenever "Network" == "Moburst_Chartboost". i realize this should be very simple but trying out these:

dataset = read.csv('C:/Users/User/Downloads/example.csv')
for( i in 1:nrow(dataset)){
  if(dataset$Network == 'Moburst_Chartboost') dataset$Campaign <- '1055'
}

this yields an error: Warning messages:

1: In if (dataset$Network == "Moburst_Chartboost") dataset$Campaign <- "1055" :
  the condition has length > 1 and only the first element will be used
2: In if (dataset$Network == "Moburst_Chartboost") dataset$Campaign <- "1055" :
  the condition has length > 1 and only the first element will be used
etc.

then i tried:

within(dataset, {
  dataset$Campaign <- ifelse(dataset$Network == 'Moburst_Chartboost', '1055', dataset$Campaign)
})

this turned ALL 4 values in row "Campaign" into "1055" over running what was there even when condition isn't met

also this:

dataset$Campaign[which(dataset$Network == 'Moburst_Chartboost')] <- 1055

yields this error, and replaced the values in the two first rows of "Campaign" with NA:

Warning message:
In `[<-.factor`(`*tmp*`, which(dataset$Network == "Moburst_Chartboost"),  :
  invalid factor level, NA generated

scratching my head here. new to R but this shouldn't be so hard :(

Aucun commentaire:

Enregistrer un commentaire