[R] Best method to add unit information to dataframe ?

bruno Piguet bruno.piguet at gmail.com
Mon Oct 3 16:35:33 CEST 2011


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))
}



More information about the R-help mailing list