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
______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.