[Rd] Wishlist: merge and subset to keep attributes (PR#8658)

groemping@tfh-berlin.de groemping at tfh-berlin.de
Sun Mar 5 09:50:08 CET 2006


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


When importing data from SPSS, it is a nice feature of the package foreign that
it allows (option use.value.labels=F) to work with the original SPSS codes while
keeping the value labels as information in an attribute. Unfortunately, after
merging or subsetting, these attributes disappear. 
The code below illustrates the problem: Variable time originally has value
labels that are gone after merging or subsetting.

It would be very helpful, if this could be changed.

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"
data2 <- data.frame(id=rep(c("Id1","Id2","Id3","Id4","Id5","Id6"),2), 
        y=rnorm(12))
merged <- merge(data1,data2)
subset <- subset(data1,id %in% c("Id2","Id4","Id6"))
str(merged)
## gives the output:
## `data.frame':   12 obs. of  3 variables:
##  $ id  : Factor w/ 6 levels "Id1","Id2","Id3",..: 1 1 2 2 3 3 4 4 5 5 ...
##  $ time: num  3 3 4 4 3 3 5 5 9 9 ...
##  $ y   : num  -0.621 -2.617 -0.980  0.486 -0.558 ...
str(subset)
## gives the output:
## `data.frame':   3 obs. of  2 variables:
## $ id  : Factor w/ 6 levels "Id1","Id2","Id3",..: 2 4 6
## $ time: num  4 5 4



More information about the R-devel mailing list