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
2
votes
1 answer
Efficient way to conditionally edit value labels
I'm working with survey data containing value labels. The haven package allows one to import data with value label attributes. Sometimes these value labels need to be edited in routine ways.
The example I'm giving here is very simple, but I'm…

John J.
- 1,450
- 1
- 13
- 28
2
votes
2 answers
Changing internal factor levels in R (important for haven - write_dta())
The haven package provides a very useful function for exporting a data frame/Tibble to Stata using the write_dta() function.
When an R factor is written into Stata (using the write_dta() function) the internal R factor levels become the numerical…

STP
- 45
- 4
2
votes
1 answer
Reading haven created dta file in Stata - how to deal with dots in variable names?
We are working in Stata with data created in R, that have been exported using haven package. We stumbled upon an issue with variables that have a dot in the name. To replicate the problem, some minimal R code:
library("haven")
var.1 <-…

radek
- 7,240
- 8
- 58
- 83
2
votes
1 answer
Efficiently converting labelled variables to factors
I'm struggling with ways to efficiently turn labelled variables into factors. The dataset I'm working with is available from here:
[https://www.dropbox.com/s/jhp780hd0ii3dnj/out.sav?dl=0][1].
It was an spss data file, which I like to use because of…

spindoctor
- 1,719
- 1
- 18
- 42
1
vote
1 answer
pivot_longer() with haven variable labels
I have a tibble whose columns are labelled using haven, like so:
library(tibble)
library(tidyr)
library(labelled)
tibble(
id = 101:102,
var_1 = c(1, 2),
var_2 = c(3, 4)
) %>%
set_variable_labels(var_1 = "Variable 1", var_2 = "Variable 2")…

RoyalTS
- 9,545
- 12
- 60
- 101
1
vote
2 answers
How to encrypt a SPSS-file using cypher
Is there a way to encrypt SPSS-files (.sav) using the cyphr-package? Encrypting .csv works fine, but when I try to encrypt .sav, I get following error-message:
Error in db_lookup(dat$ns, dat$name, file_arg) :
Rewrite rule for haven::write_sav…

Dierforth
- 81
- 6
1
vote
0 answers
Method for recoding all user-defined missing values from SPSS to NA during import in R?
I am using the haven package to import a large SPSS dataset into R. The dataset contains many variables with unique user-defined missing values--sometimes several for a single variable.
The date/time variables share 8888-08-08 and 9999-09-09 as…

Bren
- 33
- 4
1
vote
1 answer
How can I convert to dataframe, but drawing in data labels rather than content?
I've switched recently from using CSV or XLSX outputs from qualtrics surveys to using SAV format data which I can analyse in SPSS and then pass on to R. One key challenge for R is the ways that survey data can appear as numbered responses ("1", "2",…

Jeremy Kidwell
- 210
- 1
- 11
1
vote
1 answer
How to read plain fixed width SPSS data with control syntax files in R
The PISA datasets for 2000-2012 are SPSS format txt files with fixed width data columns, there are accompanying SPSS control files (syntax files?) that tell you how to parse the data. I can't seem to find a way for R to ingest this data, I've looked…

pluke
- 3,832
- 5
- 45
- 68
1
vote
1 answer
Error "t.haven_labelled()` not supported" when trying to substitute NA with mice package
Total R noob here, trying to figure out how to implement mice package to account for NAs in my dataset.
This is my code so far (i left out the unimportant stuff like trimming the data set down to relevant variables, recoding…

Moritary
- 21
- 3
1
vote
1 answer
Creating a codebook in R that considers non-labelled variables
I found a useful and simple function which creates a codebook for .dta datasets in R, the code is the following:
codebook <- map_df(dt, function(x) attributes(x)$label) %>%
gather(key = Code, value = Label) %>%
mutate(Type = map_chr(dt,…

tommaso crosta
- 15
- 4
1
vote
1 answer
How to recode values in haven_labelled vectors in R
I am working with data imported from SPSS using the haven package, imported using read_sav().
The data exists in columns of class haven_labelled, which is somewhat similar to a factor in that it contains a value and a label but is different in other…

AWaddington
- 725
- 8
- 18
1
vote
1 answer
How to tag all missing values of an SPSS labelled dataframe by their label?
I have labeled SPSS data like this:
library(labelled)
library(tidyverse)
test <- tibble(
var_1 = labelled_spss(
c(1:4, 89,999),
c(Terrible = 1, Meh = 2, Better = 3, Awesome = 4, dk = 89, "does not apply" = 999)
),
var_2 =…

Tea Tree
- 882
- 11
- 26
1
vote
1 answer
R import of stata file has problems with French accented characters
I have a large stata file that I think has some French accented characters that have been saved poorly.
When I import the file with the encoding set to blank, it won't read in. When I set it to latin1 it will read in, but in one variable, and I'm…

spindoctor
- 1,719
- 1
- 18
- 42
1
vote
1 answer
How do I create a loop to change the text encoding of the labels in labelled variables in R
I have imported a stata file that is giving me some encoding problems in the value labels. On import, using labelled::lookfor for any keyword returns this error:
Error in structure(as.character(x), names = names(x)) :
invalid multibyte string at…

spindoctor
- 1,719
- 1
- 18
- 42