13

I have imported an SPSS .sav file using the library foreign read.spss function.

dataset = read.spss("data.sav", to.data.frame=TRUE)

I want to access the column descriptions but can't work out how to access them programmatically. I can see these in the data viewer in RStudio, in the header just below the bold column names.

Image here: https://i.stack.imgur.com/PgIO5.png

Caspar Addyman
  • 151
  • 1
  • 1
  • 7

2 Answers2

21

You may be better off importing the data using the read_sav function from the haven package (another great package from Hadley Wickham).

Read a 'sav' file:

dd <- read_sav("SomeFile.sav")

head(dd)[,1:10]

methods(as_factor)
table(dd$District)
class(dd$District)
class(dd$Date)
lapply(dd, class)   # some variables have labels and others don't
lapply(dd, class) %>% head

The 'labelled' variables have attributes to show their variable label ('label') and their value labels ('labels')

dd$Region
attributes(dd$Region)

You can read a variable label:

attr(dd$Region, 'label')

You can change a variable label:

attr(dd$Region, 'label') <- 'a new label for Region'
attr(dd$Region, 'label')

same for value labels

attr(dd$Region, 'labels')

to change the names, you need to change the 'names' of the attribute

names(attr(dd$Region, 'labels')) <- c("NE","Nyanza","West")
attr(dd$Region, 'labels')
scribbles
  • 4,089
  • 7
  • 22
  • 29
  • 2
    `attributes(dd$Region)` gets the label for only 1 column. How do we do that complete dataframe and same in a separate row or column into a separate row or dataframe ? – user5249203 Apr 02 '21 at 19:47
2

The list of variable labels can be accessed with attributes(dataset)$variable.labels.

maccruiskeen
  • 2,748
  • 2
  • 13
  • 23