Best method to add unit information to dataframe ?
On Mon, Oct 3, 2011 at 10:35 AM, bruno Piguet <bruno.piguet at gmail.com> wrote:
Dear all,
?I'd like to have a dataframe store information about the units of
the data it contains.
?You'll find below a minimal exemple of the way I do, so far. I add a
"units" attribute to the dataframe. But ?I dont' like the long syntax
needed to access to the unit of a given variable (namely, something
like :
? var_unit <- attr(my_frame, "units")[[match(var_name, attr(my_frame,
"names"))]]
?Can anybody point me to a better solution ?
Thanks in advance,
Bruno.
# Dataframe creation
x <- c(1:10)
y <- c(11:20)
z <- c(101:110)
my_frame <- data.frame(x, y, z)
attr(my_frame, "units") <- c("x_unit", "y_unit")
#
# later on, using dataframe
for (var_name in c("x", "y")) {
? idx <- match(var_name, attr(my_frame, "names"))
? var_unit <- attr(my_frame, "units")[[idx]]
? print (paste("max ", var_name, ": ", max(my_frame[[var_name]]), var_unit))
}
The Hmisc package has some support for this: library(Hmisc) DF <- data.frame(x, y, z) units(DF$x) <- "my x units" units(DF$y) <- "my y units" units(DF$x)
Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com