[R] From list to variable name
Michelle Greve
michelle_greve at yahoo.com
Tue Jul 14 16:21:21 CEST 2009
Hello
I am trying to run equations with different combinations of explanatory variables. I have managed to figure out how to generate the different combinations, and am trying to set up equations where I use these combinations. And this is where I am getting stuck. I have searched several R websites but with no success.
This is how I generate the different combinations of my explanatory variables and replace the resulting colons with '+':
> comb<-cbind(attributes(terms(~ pop_1850AD * pop_500AD * pop_0AD * pop_1000AD * pop_1000BC))$term.labels)
> x<-gsub(":","+",comb)
I think, what I need to do next, is get rid of the inverted commas. So I have tried several things.
1. w<-print(x,quote=F) - this prints out the list without inverted commas, but if I then type 'w' again, the inverted commas have remained.
2. I have subsequently tried the following loop:
> y<-vector(mode = "list", length = length(x))
> for(i in 1:length(x)){
> y[[i]]<-as.name(x[i])
> }
This created a vector with e.g.:
> y[[7]]
`pop_1850AD+pop_0AD`
So, I tried putting this into the equation I'm building:
> hh[[7]]<-lrm(as.factor(ap)~y[[7]])
only to the following fault message:
Error in model.frame.default(formula = as.factor(ap) ~ y[[1]], na.action = function (frame) :
invalid type (symbol) for variable 'y[[1]]'
Is there anyway I can build these equations without typing them in by hand? I am not sure where I'm going wrong or what other search terms to use on the help list.
Thank you,
Michelle
More information about the R-help
mailing list