Vito Muggeo
2024-May-08 11:47 UTC
[R] losing variable attributes when subsetting model.frame
dear all,
I have a simple function f() which, when included in model.frame() via
the formula, returns the variable itself with some attributes.
However when I specify the subset argument, the attributes get lost,
apparently.
I would like to extract the attributes also when specifying the subset
argument. Of course, I can build the whole dataframe without subsetting,
taking the attributes and then build again the dataframe with 'subset',
but I am wondering if a more direct (and elegant) solution exists.
Any suggestion?
Thank you very much,
best,
Vito
#============================Here a simple example..
f<- function(x){
attr(x,"vi")<-length(x)
x
}
x<- 1:5
z<-runif(5)
y<-rnorm(5)
mf<- model.frame(y~f(z))
attr(mf[,2],"vi") #it works
mf <- model.frame(y~f(z), subset=x>=3)
attr(mf[,2],"vi") #it does not work
--
================================================Vito M.R. Muggeo, PhD
Professor of Statistics
Dip.to Sc Econom, Az e Statistiche
Universit? di Palermo
viale delle Scienze, edificio 13
90128 Palermo - ITALY
tel: 091 23895240; fax: 091 485726
http://www.unipa.it/persone/docenti/m/vito.muggeo
Assoc Editor: Statist Modelling
past chair, Statistical Modelling Society
coordinator, PhD Program in Econ, Businss, Statist
Duncan Murdoch
2024-May-08 12:06 UTC
[R] losing variable attributes when subsetting model.frame
On 08/05/2024 7:47 a.m., Vito Muggeo via R-help wrote:> dear all, > I have a simple function f() which, when included in model.frame() via > the formula, returns the variable itself with some attributes. > However when I specify the subset argument, the attributes get lost, > apparently. > > I would like to extract the attributes also when specifying the subset > argument. Of course, I can build the whole dataframe without subsetting, > taking the attributes and then build again the dataframe with 'subset', > but I am wondering if a more direct (and elegant) solution exists. > > Any suggestion? > Thank you very much, > best, > Vito > > > #============================> Here a simple example.. > > f<- function(x){ > attr(x,"vi")<-length(x) > x > } > > x<- 1:5 > z<-runif(5) > y<-rnorm(5) > > mf<- model.frame(y~f(z)) > attr(mf[,2],"vi") #it works > > > mf <- model.frame(y~f(z), subset=x>=3) > attr(mf[,2],"vi") #it does not work >I would guess that subsetting uses [] indexing on each column, with a logical argument. If that is true, then one solution (which is less direct, but maybe someone would call it elegant) is to put a class on the result of `f()`, and define a `[` method for that class that preserves the attributes you want to preserve. In your example: f <- function(x) { attr(x, "vi") <- length(x) class(x) <- c("withAttributes", class(x)) x } `[.withAttributes` <- function(x, i) { subset <- NextMethod() attr(subset, "vi") <- attr(x, "vi") subset } x<- 1:5 z<-runif(5) y<-rnorm(5) mf <- model.frame(y~f(z), subset=x>=3) attr(mf[,2],"vi") #it works #> [1] 5 Duncan Murdoch