strengejacke / sjlabelled

Working with Labelled Data in R

Home Page:https://strengejacke.github.io/sjlabelled

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

write_spss not working with integer vectors

andresimi opened this issue · comments

Hi, I noticed a problem when we label integer vectors and export data with write_spss.

labelling numeric vectors

library(rio)
library(sjlabelled)
library(tidyverse)

data <- mtcars %>% 
  as_tibble() %>%
  set_labels(vs:carb, labels = c(zero=0, one=1, two=2, three=3, four=4))
data %>% glimpse()
#> Rows: 32
#> Columns: 11
#> $ mpg  <dbl> 21.0, 21.0, 22.8, 21.4, 18.7, 18.1, 14.3, 24.4, 22.8, 19.2, 17.8,…
#> $ cyl  <dbl> 6, 6, 4, 6, 8, 6, 8, 4, 4, 6, 6, 8, 8, 8, 8, 8, 8, 4, 4, 4, 4, 8,…
#> $ disp <dbl> 160.0, 160.0, 108.0, 258.0, 360.0, 225.0, 360.0, 146.7, 140.8, 16…
#> $ hp   <dbl> 110, 110, 93, 110, 175, 105, 245, 62, 95, 123, 123, 180, 180, 180…
#> $ drat <dbl> 3.90, 3.90, 3.85, 3.08, 3.15, 2.76, 3.21, 3.69, 3.92, 3.92, 3.92,…
#> $ wt   <dbl> 2.620, 2.875, 2.320, 3.215, 3.440, 3.460, 3.570, 3.190, 3.150, 3.…
#> $ qsec <dbl> 16.46, 17.02, 18.61, 19.44, 17.02, 20.22, 15.84, 20.00, 22.90, 18…
#> $ vs   <dbl> 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0,…
#> $ am   <dbl> 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0,…
#> $ gear <dbl> 4, 4, 4, 3, 3, 3, 3, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 4, 4, 4, 3, 3,…
#> $ carb <dbl> 4, 4, 1, 1, 2, 1, 4, 2, 2, 4, 4, 3, 3, 3, 4, 4, 4, 1, 2, 1, 1, 2,…

data %>% write_spss("car.sav")
#> Tidying value labels. Please wait...
#> Writing spss file to 'car.sav'. Please wait...

Everything seems fine.

labelling integer vectors

However, when we set labels to integer vectors:

data <- mtcars %>% 
  as_tibble() %>%
  mutate(across(vs:carb, as.integer)) %>% 
  set_labels(vs:carb, labels = c(zero=0, one=1, two=2, three=3, four=4))
data %>% write_spss("car.sav")
#> Tidying value labels. Please wait...
#> Writing spss file to 'car.sav'. Please wait...
#> Error: Invalid input type, expected 'integer' actual 'double'

Created on 2021-06-10 by the reprex package (v2.0.0)

Thanks, should work now.

I have the same error. Maybe I need to update to the github version and not use cran?

Yes, not yet submitted to CRAN, but I can actually do that the next days...

Thank you! That would be great.

I have version 1.18.1. I think this is the latest release but unfortunately I still have the same error. Do you maybe have an idea why this could be?

Do you use the CRAN or GitHub version?

I think I'm using the GitHub version but not 100% sure. I now also removed the sjlabelled folder from "R-4.0.4\library" because I thought that could be a problem. I installed again with "devtools::install_github("strengejacke/sjlabelled")" but still the same error.
Could there be a conflict with the CRAN version that I had before that I can't find?

Sometimes the same error still comes up. Not with every data frame though. Don't see a difference in the data frames that could cause that. I have the GitHub version installed.

I'm not sure, but I think this happens with variables that have completely missing values. Can you confirm that? (i.e. it works after removing all variables with only NA, and then save the data?)

Thank you! It worked. FYI: I used remove_empty() from the janitor package to remove "empty" (= all NAs) columns.

Should be fixed in this commit: b00d6b7

The bug showed up for me again. I checked the dataset with two methods and there are no columns with only NAs. Also I tried the CRAN version and the commit you posted before. I'm out of ideas.