Use this tag for questions related to the `haven` package for the R programming language. The `haven` package "enables R to read and write various data formats used by other statistical packages". For more information on the `haven` package see the [package website](https://haven.tidyverse.org/index.html).
Questions tagged [r-haven]
159 questions
17
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…

spindoctor
- 1,719
- 1
- 18
- 42
17
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(*,…

andrewH
- 2,281
- 2
- 22
- 32
15
votes
6 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…

Heisenberg
- 8,386
- 12
- 53
- 102
14
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…

ZRoss
- 1,437
- 1
- 15
- 32
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…

s_baldur
- 29,441
- 4
- 36
- 69
6
votes
2 answers
"Error: Can't convert to character"
My student and I both loaded the same dataset, installed the same packages, and were running the same code. When I run "frq" I get a frequency table and the variable is labeled as "numeric." When my student runs the same code, she gets "Error: Can't…

mt646
- 61
- 1
- 2
6
votes
4 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…

Heisenberg
- 8,386
- 12
- 53
- 102
5
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,…

DanG
- 689
- 1
- 16
- 39
5
votes
3 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…

Wilcar
- 2,349
- 2
- 21
- 48
5
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 =…

Ruben
- 3,452
- 31
- 47
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…

rluech
- 606
- 4
- 15
4
votes
0 answers
How to open a password protected .sav (spss) file in R
I have a .sav (SPSS) data file. The file is password protected, and I have the password. I do not have SPSS.
I typically use the haven package (read_sav()) to import .sav files into R; however, I cannot find a reference for how to unlock the file…

AWaddington
- 725
- 8
- 18
4
votes
3 answers
Error in UseMethod("rescale") : no applicable method for 'rescale' applied to an object of class "c('haven_labelled', 'vctrs_vctr', 'double')"
I am getting the following error:
Error in UseMethod("rescale") : no applicable method for 'rescale'
applied to an object of class "c('haven_labelled', 'vctrs_vctr',
'double')"
Here is my code for the plot:
ggplot(data_q_agg3, aes(x = 'qmrcms', y…

Luke Thompson
- 41
- 1
- 2
4
votes
3 answers
Am I the only one experiencing problems with R's haven package?
I want to open a stata file in R, via haven::read_dta(). I'have created my script a few weeks ago and it all worked well until now: suddenly, when I try loading the data my session crashes ("R Session Aborted"). A colleague of mine is having the…

micsky
- 113
- 12
4
votes
1 answer
Haven: read_dta error() "Failed to parse /Users/folder/my_data.dta: Unable to allocate memory."
I have a relatively large .dta file, with 1280000 observations, that works fine in Stata, but I am having troubles importing it into R.
The data was created with Stata 15, the data contains strL or str#, #>244 variables and cannot be saved in the…

Alex
- 1,207
- 9
- 25