Bug 8659 - Wishlist: xtabs and table to optionally use attribute value labels
Summary: Wishlist: xtabs and table to optionally use attribute value labels
Status: NEW
Alias: None
Product: R
Classification: Unclassified
Component: Wishlist (show other bugs)
Version: old
Hardware: ix86 (32-bit) Windows 32-bit
: P5 normal
Assignee: Jitterbug compatibility account
URL:
Depends on:
Blocks:
 
Reported: 2006-03-05 15:22 UTC by Jitterbug compatibility account
Modified: 2006-03-05 15:22 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jitterbug compatibility account 2006-03-05 15:22:54 UTC
From: groemping@tfh-berlin.de
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 

Comment 1 Jitterbug compatibility account 2006-03-13 16:16:00 UTC
NOTES:
 User has been asked to provide patch for 2.3.0
Comment 2 Jitterbug compatibility account 2006-03-13 16:16:14 UTC
Audit (from Jitterbug):
Mon Mar 13 11:16:14 2006	ripley	changed notes
Mon Mar 13 10:16:14 2006	ripley	moved from incoming to wishlist