Wishlist: xtabs and table to optionally use attribute value labels (PR#8659)

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

Wishlist: xtabs and table to optionally use attribute value labels (PR#8659)

Ulrike Grömping-2
Full_Name: Ulrike Grömping
Version: 2.2.1
OS: Windows
Submission from: (NULL) (

A wish somehow related to my wish 8658: Package foreign allows to import
categorical data from SPSS (and possibly other software) using the original
codes, which are often useful for data manipulation, since one can use already
available lists of codes from others who don't use R etc. The original value
labels are preserved as an attribute of the variables. It would be very nice if
these value labels instead of the codes could be displayed in tables. The code
at the end of this note illustrates what I mean; currently I can only achieve a
conveniently annotated table by generating a factor from each variable I want to
tabulate with a lengthy command.

My wish: being able to generate the last table in the example output by a
statement like 'table(data1$time)' or 'table(data1$time, use.value.labels=T)' or


'table(as.factor(data1$time))' would do the job, if 'as.factor()' would use
value labels (like in
  data1$ftime <- factor(data1$time,levels=attr(data1$time, "value.labels"),
), whenever they are present. This might also help in obtaining more meaningful
output e.g. from regression procedures with such data.

Hoping this wish will make it to the list of wishes fulfilled some day.

With kind regards, Ulrike
data1 <- data.frame(id=c("Id1","Id2","Id3","Id4","Id5","Id6"),
vallab <- c(3,4,5,9)
names(vallab) <- c("day","night","twilight","unknown")
## gives the output:
## `data.frame':   6 obs. of  2 variables:
##  $ id  : Factor w/ 6 levels "Id1","Id2","Id3",..: 1 2 3 4 5 6
##  $ time: atomic  3 4 3 5 9 4
##   ..- attr(*, "value.labels")= Named num  3 4 5 9
##   .. ..- attr(*, "names")= chr  "day" "night" "twilight" "unknown"
## gives the output:
## 3 4 5 9
## 2 2 1 1
## gives the output:
##      day    night twilight  unknown
##        2        2        1        1

[hidden email] mailing list