r - Accessing variables initialized inside eval -


can explain how access variables created within eval statement? know there many other solutions problem, trying understand why running scope issue here.

error

error in paste0("fit<-summary(lm(npisplit~", x, ",data=df));rs<-round(fit$r.squared,3);pv<-round(fit$coefficients[,4],3);if(pv<0.05){paste(",  :  object 'rs' not found  

code

df<-data.frame(npisplit=c(0,0,0,1,1,1),happiness=c(2,5,6,8,7,4)) moods<-c('happiness') lapply(moods,function(x){     eval(parse(text=paste0(         "fit<-summary(lm(npisplit~",x,",data=df));rs<-round(fit$r.squared,3);pv<-round(fit$coefficients[,4],3);if(pv<0.05){paste(",x,"=",rs,"&",pv,")}")))     }) 

you need escape quotes inside of quotes.

x <- 1 lapply(1:2, function(.) eval(parse(text="y<-x+1;paste(\"y=\", y)"))) 

Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -