[R] Propagate vector attributes to data frame

Xavier Robin Xavier.Robin at unige.ch
Wed Oct 29 16:33:07 CET 2008


Hello,

I've got a function that takes a numeric vector (x), computes a
transformation value (myAttr) for x, transforms x according to myAttr
and then sets myAttr as an attribute of x before returning x, so I can
easily know what myAttr was used (basically it's a power transformation
and myAttr is the lambda).

myFunction.numeric <- function(x, ...) {
	myAttr <- calcMyAttr(x, ...)
	x <- myTransform(x, myAttr, ...)
	attr(x, "myAttr") <- myAttr
	return(x)
}

To apply it over several columns of a data.frame, I have the following
function:

myFunction.data.frame <- function(df, ...) {
  df <- apply(df, 2, myFunction , ...)
}

myDataFrame[cols] <- myFunction(myDataFrame[cols])

Unfortunately, it seems to me that the attributes set in myFunction are
not transferred into the data frame. At least I can't figure out how to
extract them. attributes(myDataFrame$myColum) is NULL and there is
nothing in attributes(myDataFrame) that looks like a possible myAttr.

Is there a way so that attributes of numeric vectors could be kept in
the data.frame?

Thanks in advance,
Xavier Robin

-- 
Xavier Robin

Biomedical Proteomics Research Group (BPRG)
Department of Structural Biology and Bioinformatics (DBSB)
Geneva University Medical Center (CMU)
1, rue Michel Servet - CH-1211 Genève 4 - Switzerland
Tel: (+41 22) 379 53 21
Fax: (+41 22) 379 59 84
Xavier.Robin at unige.ch




More information about the R-help mailing list