0

I use

df <- read_sav("data/file.sav", user_na = FALSE)

But the data does not look like expected

col1   col2
1      Onestreet
2      Twostreet
NA
4    
5

How come rows 3-5 not become NA in col2?

Maël
  • 45,206
  • 3
  • 29
  • 67
Victor Nielsen
  • 443
  • 2
  • 14
  • Not sure what you expect. And your question is difficult to answer without knowing your data. You set the parameter user_na to FALSE the default), which means that R will ignore any user-specified (!) value from your data set and instead convert it to NA! Your col2 looks like a string column and in case row 3-5 are empty they will show as empty (i.e. ““), not as missings. – deschen Mar 07 '22 at 09:16
  • Ah so if they are string data, they will never show as NA? I thought the user_na will also make empty values into NA – Victor Nielsen Mar 07 '22 at 10:33
  • I can‘t recollect grom the top of my head if you can specify user-missings for string variables in SPSS. You could try if you have access to SPSS. If this foesn‘t work, please see my answer foe an option to replace empty values by missings. – deschen Mar 07 '22 at 10:36

1 Answers1

0

To convert the empty strings to missing you can do:

library(dplyr)
df %>%
  mutate(col2 = if_else(col2 == '', NA_character_, col2))
deschen
  • 10,012
  • 3
  • 27
  • 50
  • Actually, when I try to do that, it doesn't actually happen. The code doesn't make errors, but when I look in the table, the empty fields are still just empty (no NA). – Victor Nielsen Mar 07 '22 at 11:51
  • Then please provide an example of your data so that we can check: https://stackoverflow.com/help/minimal-reproducible-example – deschen Mar 07 '22 at 11:53