The Hmisc package lets you assign labels to data. This information is not included when using write.spss from the "foreign" package. This function tries to address that.
write.Hmisc.SPSS(data, datafile, codefile)
data | The input data.frame |
---|---|
datafile | The name for the resulting SPSS data file |
codefile | The name for the resulting SPSS code file |
Two files will be created in your working directory: a script file and a data file that can be used with SPSS
http://stackoverflow.com/a/10261534/1270695
Ananda Mahto. Includes code from Chuck Cleland
df <- data.frame(id = c(1:6), p.code = c(1, 5, 4, NA, 0, 5), p.label = c('Optometrists', 'Nurses', 'Financial analysts', '<NA>', '0', 'Nurses'), foo = LETTERS[1:6]) # Add some variable labels using label from the Hmisc package library(Hmisc)#>#>#>#>#> #>#>#> #>label(df) <- "Sweet sweet data" label(df$id) <- "id blahblah" label(df$p.label) <- "Profession with human readable information" label(df$p.code) <- "Profession code" label(df$foo) <- "Variable label for variable x.var" # modify the name of one varibe to see what happens when exported names(df)[4] <- "New crazy name for 'foo'" df#> id p.code p.label New crazy name for 'foo' #> 1 1 1 Optometrists A #> 2 2 5 Nurses B #> 3 3 4 Financial analysts C #> 4 4 NA <NA> D #> 5 5 0 0 E #> 6 6 5 Nurses F#> [1] "downlit" "file96622194e8cf9" "file966221f98c176.so" #> [4] "file9662251fc5f92" "file966225935567d" "file966227fa7d859" #> [7] "file966227faa6f23" "file96622f5c4a8f.txt"write.Hmisc.SPSS(df, "df.sav", "df.sps")#> Warning: some variable names were abbreviated#> 1 1 5 1 #> 2 5 4 2 #> 3 4 3 3 #> 4 NA 1 4 #> 5 0 2 5 #> 6 5 4 6#> DATA LIST FILE= “df.sav” free #> / id p.code p.label Nwcnf'f' . #> #> VARIABLE LABELS #> id "id blahblah" #> p.code "Profession code" #> p.label "Profession with human readable information" #> Nwcnf'f' "Variable label for variable x.var" #> . #> #> VALUE LABELS #> / #> p.label #> 1 "<NA>" #> 2 "0" #> 3 "Financial analysts" #> 4 "Nurses" #> 5 "Optometrists" #> / #> Nwcnf'f' #> 1 "A" #> 2 "B" #> 3 "C" #> 4 "D" #> 5 "E" #> 6 "F" #> . #> #> EXECUTE.#> [1] TRUE TRUEsetwd(x)