Questions tagged [r-haven]

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).

159 questions
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",…
MYaseen208
  • 22,666
  • 37
  • 165
  • 309
4
votes
3 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…
Matt
  • 41
  • 4
4
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 ... ..-…
Joshua Rosenberg
  • 4,014
  • 9
  • 34
  • 73
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.…
supersambo
  • 811
  • 1
  • 9
  • 25
3
votes
2 answers

Label missing values as any other strings

I create a labelled column with haven. library(dplyr) library(haven) df <- tibble(x = labelled(c(1:3, NA), c('a' = 1, 'b' = 2, 'missing' = NA))) # # A tibble: 4 × 1 # x # # 1 1 [a] # 2 2 [b] # 3 3 # 4 NA I…
user18894435
  • 373
  • 1
  • 10
3
votes
1 answer

Convert variable label for labeled numeric variable to a new character variable

When importing a dataset from Stata to R, it often comes with helpful labels for numeric variables. I would like to be able to convert the data in the labels to a new separate variable. The equivalent command in Stata is…
Jack Landry
  • 138
  • 8
3
votes
1 answer

Error in df_parse_dta_file(): Failed to parse C:/Users/folder/data.dta: This version of the file format is not supported

I wanted to read in a .dta file in R in order to convert it to a .csv file. First, I tried to do so by using the foreign package, but it reported: Error in read.dta(file): not a Stata version 5-12 .dta file So I tried to do it by using teh haven…
stats19
  • 31
  • 1
  • 3
3
votes
1 answer

How do I add SAS/SPSS-format labels stored as a .txt file to R?

I have a large survey that I am working with. The main survey data is stored as a CSV file. The variable and value label files are included in both SAS-format as well as SPSS-format, but stored as TXT files. I have seen that there are several…
anguyen1210
  • 513
  • 5
  • 21
3
votes
1 answer

How do I use mutate_at with set_value_labels to change the value labels of multiple variables?

How do I set the value labels for multiple variables in a data frame imported from SPSS via haven. This produces variables of the haven_labelled class. I am trying to set the value labels with the [labelled][1] package . #Fake…
spindoctor
  • 1,719
  • 1
  • 18
  • 42
3
votes
2 answers

Difficulty with haven package Reading SPSS data in R

I am trying to use the haven package to read spss data and am getting the following error: Error in df_parse_sav_file(spec, encoding, user_na, cols_skip, n_max, : Failed to parse ~FilePath/Dataset: The file's timestamp string is …
hcav
  • 41
  • 2
3
votes
1 answer

Dynamically create value labels with haven::labelled

I am using haven::labelled to set value labels of a variable. The goal is to create a fully documented dataset I can export to SPSS. Now, say I have a df value_labels of values and their value labels. I also have i df df_data with variables to…
oskjerv
  • 198
  • 7
3
votes
1 answer

Exported file from R using `haven` cannot be opened by SAS

When exporting data from R using haven::write_sas(), the resulting sas7bdat file is not recognized (i.e. cannot be loaded) by SAS EG/9.4. Although there are several other packages such as foreign that provide alternative approaches, I was hoping to…
al-obrien
  • 1,353
  • 11
  • 29
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…
A Toll
  • 647
  • 7
  • 15
2
votes
2 answers

ifelse removes labels of a labelled variable

I'm fairly new to R and trying to recode missing values (stored as -99) to NA. For some reasons this removes all my variable labels from the data frame. My code is the following df <- df %>% mutate(across(everything(), ~ ifelse(. == -99, NA,…
Stephan
  • 111
  • 6
2
votes
3 answers

Create column with labels from labelled data

I have a data set with labelled data and would like to create a new column containing only the label as character. Consider the following example: value_labels <- tibble(value = 1:6, labels = paste0("value", 1:6)) df_data <- tibble(id = 1:10, var =…
Ivo
  • 3,890
  • 5
  • 22
  • 53
1
2
3
10 11