[R] Coercing a list of variables in a function call

Dimitris Rizopoulos d.rizopoulos at erasmusmc.nl
Mon Dec 1 13:21:49 CET 2008

try the following:

model.it <- function (form, data, factor.id) {
     if (!missing(factor.id) && all(factor.id %in% names(data)))
         data[factor.id] <- lapply(data[factor.id], factor)
     glm(form, data = data)

y <- c(1,2.1,3.3,4.3,5,6.5)
x1 <- c(1,1,1,2,2,2)
x2 <- c(1,2,3,1,2,3)
x3 <- c(1,3,2,4,2,4)
d <- data.frame(y, x1, x2, x3)

model.it(y ~ x1 + x2 + x3, d, c("x1", "x2"))
model.it(y ~ x1 + x2 + x3, d)

If you only plan to fit linear regression models, then it is advisable 
that you lm() instead of glm(). I hope it helps.


Philip Whittall wrote:
> This is hopefully a trivial problem for list subscribers, but I am very
> new to writing R functions.
> I wish to call an R function written by myself from another program to
> fit a model. I need
> to tell it which of the independent variables are factors. I need to do
> this in a generic way, 
> so that when the list is passed, R will work through the variables in
> the data frame and coerce them into being factors.
> This is what I tried ...
> # Fictitious data for illustration
> y<-c(1,2.1,3.3,4.3,5,6.5)
> x1<-c(1,1,1,2,2,2)
> x2<-c(1,2,3,1,2,3)
> x3<-c(1,3,2,4,2,4)
> d<-as.data.frame(cbind(y,x1,x2,x3))
> #Function definition
> model_it=function(data=" ", model=" ", factors=list()){
>          attach(d) #Attach the data
>          formula<-as.formula(model) #Set up the model
> #Identify the factors
>          if (length(factors)>0) for(i in 1:length(factors)){
>                                    factors[i]<-as.factor(factors[i])
>                                    }
> #Fit the model
>          glm(formula, data=data)
> }
> #Function call attempted
> model_it(data=d, model="y~x1+x2+x3", factors=list(x1,x2)) 
> And this is the error message I received ...
> Error in sort.list(unique.default(x), na.last = TRUE) : 
>   'x' must be atomic for 'sort.list'
> Have you called 'sort' on a list?
> The line factors[i]<-as.factor(factors[i]) is the one responsible for
> the error. 
>>From the word 'atomic', my guess is that I have asked it to set each
> value as a factor rather than the variable, but I don't know
> what syntax to use to get it to set the variable as a factor. Any
> suggestions gratefully appreciated.
> For the record, the above was run on windows XP using R2.8.0
> This message should be regarded as confidential. If you have received this email in error please notify the sender and destroy it immediately.
> Statements of intent shall only become binding when confirmed in hard copy by an authorised signatory.  The contents of this email may relate to dealings with other companies within the Detica Group plc group of companies.
> Detica Limited is registered in England under No: 1337451.
> Registered offices: Surrey Research Park, Guildford, Surrey, GU2 7YP, England.
> 	[[alternative HTML version deleted]]
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus Medical Center

Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014

More information about the R-help mailing list