Reputation: 613
I have a csv file like
data.csv
Identity,CampaignName,Adgroupname
12,abc1,asterix
13,abc2,asterix1
frere,abc3,asterix2
xrert,dasa,trete
14,tytyt,ababa
Where the first row are the headers of the CSV file.
In the above CSV file if 'rer' appears in a string in 'Identiity' column then replace the whole string with '17' . for example 'frere' will be '17'. I can use 'gsub' to replace 'rer' , but how to replace the whole string with another string? New to R, any help is appreciated.
My desired output
Identity,CampaignName,Adgroupname
12,abc1,asterix
13,abc2,asterix1
17,abc3,asterix2
17,dasa,trete
14,tytyt,ababa
Upvotes: 1
Views: 618
Reputation: 226087
dat <- read.csv(
text=
"Identity,CampaignName,Adgroupname
12,abc1,asterix
13,abc2,asterix1
frere,abc3,asterix2
xrert,dasa,trete
14,tytyt,ababa")
By default the Identity
column is a factor, and you can't introduce new strings into a factor:
dat$Identity[grepl("rer",dat$Identity)] <- "17"
## Warning message:
## In `[<-.factor`(`*tmp*`, grepl("rer", dat$Identity), value = c(1L, :
## invalid factor level, NA generated
You can either use stringsAsFactors=FALSE
when you read the data, or (starting from the original data set)
dat$Identity <- as.character(dat$Identity)
dat$Identity[grepl("rer",dat$Identity)] <- "17"
You may then want to convert the column back into a numeric variable:
dat$Identity <- as.numeric(dat$Identity)
Upvotes: 2