[R] Loosing attributes while binding data frames ...

Tribo Laboy tribolaboy at gmail.com
Fri Apr 4 10:47:17 CEST 2008


I want to keep track of my units in the data frame. I have been
advised in the past on this list to create a new "units" attribute of
the data frame and keep the units strings there. It works fine as long
as I don't manipulate the data frame.

Here's an example:

-------------CODE BEGIN -----------------------
df1 <- data.frame(x = 1:10, y = runif(10))
attr(df1,"units") <- c("s", "kg")

df2 <- data.frame(z1 = 10*runif(10), z2 = 20*runif(10))
attr(df2,"units") <- c("Pa", "kV")

df3 <- cbind(df1,df2)

-------------CODE END-----------------------

The last line returns NULL. So I got my two data frames nicely bound
in a third, but I lost my units along the way. I can easily write my
own function "cbinddfwunits" to bind the data frames with the units,
but I am curious as what are the rules that govern this unexpected R
behaviour. I would definitely expect that if the two objects have
similar properties, then these properties should not be lost during
object manipulation, but R proves me wrong ... If someone could
explain the logic I'll be grateful.



More information about the R-help mailing list