add label attribute to objects?

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

add label attribute to objects?

Liviu Andronic
Dear all
I know that the R way of documenting things is to work on your project
in package development mode, and document each object (such as data
frames) in a *.Rd files. This should work for gurus. What about a
simpler way to document things, geared for mere mortals?

I was thinking of a label() or tag() function that could store and
retrieve an alphanumeric comment for a given object (for example,
either on 'iris' or on 'iris$Species'). Using 'sos' I found some
potential candidates.
> require(sos)
> xb <- findFn('label attribute')
found 307 matches;  retrieving 16 pages
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
> grepFn( 'label', xb)


There is Hmisc::label, which seems to address my concern, but I cannot
get it to work on data frames. Am I doing something wrong here?
> require(Hmisc)
> #works fine on vectors, but loses the label upon conversion
> age <- c(21,65,43)
> y   <- 1:3
> label(age) <- "Age in Years"
> str(age)
Class 'labelled'  atomic [1:3] 21 65 43
  ..- attr(*, "label")= chr "Age in Years"
> #fails on df
> x <- iris
> label(x)
Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species
          ""           ""           ""           ""           ""
Warning message:
In mapply(FUN = label, x = x, default = default, MoreArgs = list(self
= TRUE),  :
  longer argument not a multiple of length of shorter
> label(x) <- 'some random comment'
> label(x)
Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species
          ""           ""           ""           ""           ""
Warning message:
In mapply(FUN = label, x = x, default = default, MoreArgs = list(self
= TRUE),  :
  longer argument not a multiple of length of shorter
> #however the label attribute exists!!
> str(x)
'data.frame': 150 obs. of  5 variables:
 $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
 $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
 $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
 $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
 $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1
1 1 1 1 1 1 ...
 - attr(*, "label")= chr "some random comment"


I've also found mefa::label(), which actually does what it advertises,
but as 'label' in Hmisc it has one significant drawback: "Inheritance
is not implemented, so by any transformation of the object into
another one, the label is lost."
> #works fine on vectors, but loses the label upon conversion
> (x <- 1:10)
 [1]  1  2  3  4  5  6  7  8  9 10
> (label(x) <- "x is a vector")
[1] "x is a vector"
> str(x)
 atomic [1:10] 1 2 3 4 5 6 7 8 9 10
 - attr(*, "label")= chr "x is a vector"
> str(as.numeric(x))
 num [1:10] 1 2 3 4 5 6 7 8 9 10
> #works fine on df, but loses the label upon conversion
> x <- iris
> (label(x) <- "x is a vector")
[1] "x is a vector"
> str(x)
'data.frame': 150 obs. of  5 variables:
 $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
 $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
 $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
 $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
 $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1
1 1 1 1 1 1 ...
 - attr(*, "label")= chr "x is a vector"
> str(as.matrix(x))
 chr [1:150, 1:5] "5.1" "4.9" "4.7" "4.6" "5.0" "5.4" ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:5] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width" ...


Are any of you aware of a label() function that implements
inheritance? Also, is there a reason for not having a similar label()
implementation in base R? (Folks coming from the SPSS world would
expect it.)

Regards
Liviu


--
Do you know how to read?
http://www.alienetworks.com/srtest.cfm
http://goodies.xfce.org/projects/applications/xfce4-dict#speed-reader
Do you know how to write?
http://garbl.home.comcast.net/~garbl/stylemanual/e.htm#e-mail

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.