mercredi 28 octobre 2020

Ifelse recoding data statement applied to many datasets

I have a long list of automatically generated ifelse statements for many columns and conditions in dataset:

dat$Q5_str<-ifelse(dat$Q8.4 =="Yes"| dat$Q9.3_6 =="Maybe" & dat$Q9.11=="Europe", dat$Q10_str) 
dat$Q5_otr<-ifelse(dat$Q20.4 =="Other"| dat$Q21.7_13 =="Other" & dat$Q23.11=="Other" ,dat$Q10_otr)
...
...

I need to to modify those code to apply statements not to single dataset, but to many datasets stored in list:

ldat<-list("RegionA"=dat1, "RegionB"=dat2)

So I'm looking for a solution to update (with regexp) initial syntaxes, to more generalized function:

MyIfElse(dataset=ldat, expr='dat$Q8.4 =="Yes"| dat$Q9.3_6 =="Maybe" & dat$Q9.11=="Europe", dat$Q10_str')

but then I need probably manually parse this long string and manage everything with regexp. Is there any better approach?

Aucun commentaire:

Enregistrer un commentaire