[R] losing variable attributes when subsetting model.frame

Duncan Murdoch murdoch@dunc@n @end|ng |rom gm@||@com
Wed May 8 14:06:06 CEST 2024


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



More information about the R-help mailing list