I have a script I have to run periodically where I use a varying number of assignment statements in R, like this:
r5$NWord_1<-ifelse(r5$Match==1,NA,r5$NWord_1)
r5$NWord_2<-ifelse(r5$Match==2,NA,r5$NWord_2)
r5$NWord_3<-ifelse(r5$Match==3,NA,r5$NWord_3)
r5$NWord_4<-ifelse(r5$Match==4,NA,r5$NWord_4)
r5$NWord_5<-ifelse(r5$Match==5,NA,r5$NWord_5)
r5$NWord_6<-ifelse(r5$Match==6,NA,r5$NWord_6)
r5$NWord_7<-ifelse(r5$Match==7,NA,r5$NWord_7)
The problem is that the number of "NWord" variables changes from run to run usually between 5 and 7). I have the number of "NWord" variables stored separately as Size.
Size<-5
I have tried the following, but get() only works on objects, not columns of dataframes.
for(i in 1:Size){
get(paste("r5$NWord_",i,sep=""))<-ifelse(r5$Match==i,NA,get(paste("r5$NWord_",i,sep="")))
}
I am curious: What is the best way to automate this process so I do not have to manually run a subset of these statements every time?