How to print names of values in R

一笑奈何 提交于 2019-12-25 01:32:00

问题


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?

来源:https://stackoverflow.com/questions/27127997/how-to-print-names-of-values-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!