[R] losing variable attributes when subsetting model.frame

Vito Muggeo v|to@muggeo @end|ng |rom un|p@@|t
Wed May 8 13:47:28 CEST 2024


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



More information about the R-help mailing list