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

groemping@tfh-berlin.de groemping at tfh-berlin.de
Sun Mar 5 10:22:56 CET 2006


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


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))'.

'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"), 
         labels=names(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"),
        time=c(3,4,3,5,9,4))
vallab <- c(3,4,5,9)
names(vallab) <- c("day","night","twilight","unknown")
attr(data1$time,"value.labels")<-vallab
str(data1)
## 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"
table(data1$time)
## gives the output:
## 3 4 5 9 
## 2 2 1 1 
data1$ftime<-factor(data1$time,levels=attr(data1$time,"value.labels"),
                    labels=names(attr(data1$time,"value.labels")))
table(data1$ftime)
## gives the output:
##      day    night twilight  unknown 
##        2        2        1        1



More information about the R-devel mailing list