0

I read a SPSS file into R like this:

twobytwo <- read.spss("twobytwo.sav", use.value.labels=FALSE, to.data.frame=TRUE)

I used use.value.labels=FALSE because I don't want my variables to be turned into factors.

The data structure looks like this:

> str(twobytwo)
'data.frame':   2743 obs. of  3 variables:
 $ chm  : atomic  0 0 0 0 0 0 0 0 0 0 ...
  ..- attr(*, "value.labels")= Named num  1 0
  .. ..- attr(*, "names")= chr  "member" "none"
 $ cpv  : atomic  0 0 0 0 0 0 0 0 0 0 ...
  ..- attr(*, "value.labels")= Named num  1 0
  .. ..- attr(*, "names")= chr  "yes" "no"
 $ waves: atomic  0 0 0 0 0 0 0 0 0 0 ...
  ..- attr(*, "value.labels")= Named num  1 0
  .. ..- attr(*, "names")= chr  "1998" "1970"
 - attr(*, "variable.labels")= Named chr  "church member" "conf party votes" ""
  ..- attr(*, "names")= chr  "chm" "cpv" "waves"
 - attr(*, "codepage")= int 65001

As you can see, the variable waves is a 0-1 variable with names 1970 and 1998. However, I just can't find out how I can access these names. I'm writing a function in which I want to print these values (separately) as an output. So, based on the associated value(e.g. 1), I want to print the name (for value 1, this would be 1998).

I hope my question is not to stupid, but I just can't seem to figure it out and I can't find a related thread.

Should I use attributes?

mallet
  • 2,454
  • 3
  • 37
  • 64
pm-b
  • 158
  • 12
  • Have you tried `dimnames(twobytwo$waves)` (not tested) – akrun Nov 25 '14 at 13:28
  • 1
    Try `names(attr(twobytwo$waves,"value.labels"))[match(waves,attr(twobytwo$waves,"value.labels"))]` – nicola Nov 25 '14 at 13:50
  • Thanks nicola. Your code did not produce what I wanted, but using your code, I came to: `names(attr(twobytwo$waves,"value.labels"))[match(levels(as.factor(twobytwo$waves))[1],attr(twobytwo$waves,"value.labels"))]` in which the number 1 is what I wanted to put in (the value). – pm-b Nov 25 '14 at 15:38

0 Answers0