[R] S4 coerce as.data.frame for lm

Thomas Roth (geb. Kaliwe) hamstersquats at web.de
Tue Mar 10 16:02:42 CET 2009


#Hi,
#
#For a given class test, an object of class test cannot be used as data 
in the lm method although as.data.frame was implemented... where's my 
mistake?
#
#Suppose i have defined a S4 class test

#S4 Class test containting a slot data
#which is of type data.frame

setClass(Class = "test", representation = representation(name = 
"character", data = "data.frame")  

temp = new("test")   #temp is of class test

temp at data = faithful   #assign some data to it

#now define as.data.frame for class test
setMethod("as.data.frame", "test", function(x, row.names = NULL, 
optional = FALSE)
{

  return(x at data)

}
)

as.data.frame(temp)   #works

lm(eruptions ~ waiting, data = temp)   #doesn't work


#Thank you for any hints
#Thomas Roth



#from the lm help page
|#data| - an optional data frame, list or environment (or object 
coercible by |as.data.frame| to a data frame) containing the variables 
in the model. If not found in |data|, the variables are taken from 
|environment(formula)|, typically the #environment from which |lm| is 
called.




More information about the R-help mailing list