The `haven` package for `R`
Questions tagged [r-haven]
118 questions
16
votes
3 answers
Extract the labels attribute from "labeled" tibble columns from a haven import from Stata
Hadley Wickham's haven package, applied to a Stata file, returns a tibble with many columns of type "labeled". You can see these with str(), e.g.:
$ MSACMSZ :Class 'labelled' atomic [1:8491861] NA NA NA NA NA NA NA NA NA NA ...
.. ..- attr(*,…
![](../../users/profiles/1493743.webp)
andrewH
- 1,979
- 1
- 13
- 26
15
votes
6 answers
set missing values for multiple labelled variables
How to I set missing values for multiple labelled vectors in a data frame. I am working with a survey dataset from spss. I am dealing with about 20 different variables, with the same missing values. So would like to find a way to use lapply() to…
![](../../users/profiles/1422335.webp)
spindoctor
- 1,359
- 1
- 11
- 27
13
votes
5 answers
Convenient way to access variables label after importing Stata data with haven
In R, some packages (e.g. haven) insert a label attributes to variables (e.g. haven), which explains the substantive name of the variable. For example, gdppc may have the label GDP per capita.
This is extremely useful, especially when importing data…
![](../../users/profiles/1793442.webp)
Heisenberg
- 6,824
- 8
- 44
- 87
11
votes
1 answer
Hack to to include special characters in file path in haven::read_sav()
There seems to be an issue with the haven (1.1.1) package when including any type of special character in the file path, including just the file name.
Assuming this is a real issue I am looking for some kind of neat hack/solution to get around…
![](../../users/profiles/4552295.webp)
sindri_baldur
- 22,360
- 2
- 25
- 48
11
votes
2 answers
Variable labels in the R package Haven with SPSS
I'm trying to access the variable labels (this is the description of the variable) from an SPSS por file with the haven package. I can do it just fine with the foreign package but I'd like to use haven. Any suggestions?
# Using foreign I can get the…
![](../../users/profiles/2631089.webp)
ZRoss
- 1,271
- 1
- 12
- 28
5
votes
4 answers
blown up .sav file size using haven::write_sav()
I am writing SPSS .sav files from R using the package haven, which works very well for me in general. However I have noticed that the .sav file size written on disk using write_sav() seems to be much bigger than nescessary. Whenever I open and save…
![](../../users/profiles/6105723.webp)
rluech
- 547
- 2
- 12
5
votes
2 answers
dplyr bind_rows does not preserve variable label
haven::read_dta supports importing variable label from Stata into R using the label attribute. Rstudio also supports displaying these labels in the View pane.
However, when two data frames are bound using dplyr::bind_rows (or rbind_all), the labels…
![](../../users/profiles/1793442.webp)
Heisenberg
- 6,824
- 8
- 44
- 87
4
votes
4 answers
Combine data file and label file together to have one single labelled dataframe in R
I have two data frames, one is survey data (data.csv) and another one is label data (label.csv). Here is the sample data (My original data has about 150 variables)
#sample data
df <- tibble::tribble(
~id, ~House_member, ~dob, ~age_quota, ~work,…
![](../../users/profiles/7996904.webp)
DanG
- 813
- 12
- 25
4
votes
2 answers
How to replace values by labels in data.frames from spss files?
I have to read a sav file
I use the package haven
library(haven)
dataset<- read_sav("datafile.sav")
In the console I can see the labels :
dput(head(voyages$portdep))
structure(c(50422, 50299, 50299, 50299, NA, NA), label = "Port of…
![](../../users/profiles/4887832.webp)
Wilcar
- 1,831
- 1
- 14
- 39
4
votes
2 answers
haven::read_sav showing value labels rather than codes
I'm using haven to import a .sav file into R. I wonder how to show value labels rather than numeric codes. In the following example I want to show Species names rather than numbers 1, 2, 3.
library(haven)
path <- system.file("examples", "iris.sav",…
![](../../users/profiles/707145.webp)
MYaseen208
- 19,213
- 32
- 133
- 260
4
votes
2 answers
automagically using labels (haven semantics) in ggplot2 plots
I'm plotting data marked up using haven semantics, i.e. variables and values have labels defined via attributes.
Often, these labels are also what I want in my axis titles and ticks.
library(ggplot2)
mtcars$mpg = haven::labelled(mtcars$mpg, labels =…
![](../../users/profiles/263054.webp)
Ruben
- 3,242
- 27
- 42
4
votes
2 answers
Retain SPSS value labels when working with data
I am analysing student level data from PISA 2015. The data is available in SPSS format here
I can load the data into R using the read_sav function in the haven package. I need to be able to edit the data in R and then save/export the data in SPSS…
![](../../users/profiles/8914682.webp)
Matt
- 41
- 3
4
votes
2 answers
R haven: missing labels and label names when reading spss file
I'm using the haven package for R to read an spss file with user_na=TRUE. The file has many string variables with value labels. In R only the first of the string variables (SizeofH1) has the correct value labels assigned to it as attribute.…
![](../../users/profiles/1509010.webp)
supersambo
- 791
- 1
- 8
- 23
3
votes
2 answers
Use of haven to read .sav (SPSS_ files): Change labelled vector to character string or factor
I am using the haven library to read an .sav (SPSS) file into R.
Some of the values are read as a labelled vector.
Here is an example of one:
> str(df$instructional_practice)
Class 'labelled' atomic [1:4136] 2 2 6 6 8 8 NaN NaN 17 1 ...
..-…
![](../../users/profiles/4287974.webp)
Joshua Rosenberg
- 3,364
- 4
- 26
- 61
3
votes
1 answer
Identifying a tbl_df column class
I am reading a SAS dataset into R. SAS stores missing character values as empty quotes, but thankfully zap_empty() converts those values to NA.
My data set contains almost 400 variables, and I'd rather not check each of those individually. I would…
![](../../users/profiles/3577408.webp)
A Toll
- 507
- 5
- 13