将R data.frame导出到SPSS [英] Export R data.frame to SPSS
问题描述
有一个包含外部
的函数 write.foreign()
可以写一个SPS和CSV文件。 SPS文件可以将CSV文件读取到包含标签的SPSS中。很好,但是这个功能有一些问题:
There is a package foreign
with a function write.foreign()
that can write a SPS and CSV file. The SPS file than can read the CSV fiel into SPSS including labels. Fine so far, but there are some issues with that function:
- 较新的SPSS版本可能会显示一个错误,您的格式定义太少
数据列表
- 如果通过
attr()$ c存储的数值变量有标签 $ c>,这些都丢失了。
- 即使SPSS vesion支持高达32767的字符串,函数
write.foreign()
如果任何变量中有超过255个,则停止。 - 如果使用任何字符变量,则会显示星号(*),但较新的SPSS版本无法处理。
- CSV文件以逗号分隔,并且(可以)不使用引号,因此字符串(字符)中不允许使用逗号
- 非ASCII字符(例如,变音符号)将导致导致
- 如果您的字符包含任何NA值,您将看到...
- Newer SPSS versions may show an error that you have too few format definitions in
DATA LIST
- If there are "labels" for numeric variables stored via
attr()
, these are lost. - Even if the SPSS vesion supports strings up to 32767, the function
write.foreign()
stops if there are more than 255 in any variable. - Theres a star (*) if any character variables are used, but newer SPSS versions cannot handle that.
- The CSV file is comma-separated and does (can) not use quotes, therefore no commas are allowed in strings (character)
- Non-ASCII caracters (e.g. umlauts) will crash the import
- Should you have a character that contains any NA value, you'll see...
...这样的错误消息:
... an error message like this:
Error in if (any(lengths > 255L)) stop("Cannot handle character variables longer than 255") :
missing value where TRUE/FALSE needed
I花了很多时间,然后找到了一个很好的发布( http://r.789695.n4.nabble.com/SPSS-export-in-R-package-foreign-td921491.html )开始做得更好。这是我的结果,我想和你分享。
I spent a lot of time with that and then found a good posting (http://r.789695.n4.nabble.com/SPSS-export-in-R-package-foreign-td921491.html) to start on and make it better. Here's my result, I'd like to share with you.
推荐答案
要将R data.frame导出到SPSS,请使用 write_sav
from the haven package:
To export an R data.frame to SPSS, use write_sav
from the haven package:
library(haven)
write_sav(mtcars, "mtcars.sav")
这篇关于将R data.frame导出到SPSS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!