How to apply the same function over a series of columns with a specific
string in their names?
I have a list of variables named as av1 av2 av3…av144 in my data frame
(dat). I want to recode these into another series of variables say main1
main2 main3… main144 as such:
dat$main1<-ifelse (dat$av1==5 or dat2$av1==8 or dat$av1==6,10,0)
dat$main2<-ifelse (dat$av2==5 or dat2$av2==8 or dat$av2==6,10,0)
#…
dat$main144<-ifelse (dat1$av144==5 or dat$av144==8 or dat$av144==6,10,0)
Could anyone please tell me how can I apply this ifelse command over two
sets of variables without re-writing the same line 144 times? I have
unsuccessfully experimented with "grep" trying to extract by column names
but I think I was in the wrong direction…
Thank you very much in advance,
No comments:
Post a Comment