I have dataframe of character columns named new_sgs that looks like this:
SG.Name RegionCode
1 AW02PASGA001 01
2 AW02PASGA002 01
3 AW02PASGA003 01
4 AW02PASGA004 01
5 AW02PASGA005 01
6 AW02PASGA006 01
...
I want to replace '02' in the strings of column 1 with the string in column2. This does the job for row 1:
new_sgs$SG.Name[1] <- gsub("AW02", paste0("AW", new_sgs$RegionCode[1]), new_sgs$SG.Name[1])
Is there a way to make this change to every row using one of the apply functions? I've tried
sapply(new_sgs, function(x) gsub("AW02", paste0("AW", new_sgs$RegionCode[x]), new_sgs$SG.Name[x]))
but this is what I get:
SG.Name RegionCode
[1,] NA NA
[2,] NA NA
[3,] NA NA
[4,] NA NA
[5,] NA NA
[6,] NA NA
...
Warning messages:
1: In gsub("AW02", paste0("AW", test$RegionCode[x]), test$SG.Name[x]) :
argument 'replacement' has length > 1 and only the first element will be used
2: In gsub("AW02", paste0("AW", test$RegionCode[x]), test$SG.Name[x]) :
argument 'replacement' has length > 1 and only the first element will be used
Thanks!
Luke