rcsvspss-files

How do I convert a SAV. file to a CSV file in R?


I am trying to convert SAV. to CSV. with R. However, I got an error. My data was imported as a SAV. file, but I wonder whether the problem may be that the data is in tbl_df data.frame. Could this be a problem? What is the error and the correct coding to continue with analyses? Below codes and output. Thank you!

library(haven)

HNIR62FL_data_2 <- read_sav("~/DHS/HNIR62SV/HNIR62FL_data_2.SAV")

View(HNIR62FL_data_2)

install.packages("foreign")

library("foreign")

class(HNIR62FL_data_2)

[1] "tbl_df"     "tbl"        "data.frame"

write.table(read.spss("HNIR62FL_data_2.sav"), file = "from_sav_data.csv", quote = FALSE, sep = ",") 

Error in read.spss("HNIR62FL_data_2") : unable to open file: 'No such file or directory

Solution

  • Your error seems to be related to the missing extension in "HNIR62FL_data_2" inside write.table(...). You just need to provide it.

    Also, you don't need to keep install.packages() in your script. This should be enough:

    library(haven)
    
    HNIR62FL_data_2 <- read_sav("~/DHS/HNIR62SV/HNIR62FL_data_2.SAV")
    
    write.table(HNIR62FL_data_2, file = "from_sav_data.csv", quote = FALSE, sep = ",")