将R data.frame导出到SPSS [英] Export R data.frame to SPSS

查看:249
本文介绍了将R data.frame导出到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:


  1. 较新的SPSS版本可能会显示一个错误,您的格式定义太少数据列表

  2. 如果通过 attr(),这些都丢失了。

  3. 即使SPSS vesion支持高达32767的字符串,函数 write.foreign()如果任何变量中有超过255个,则停止。

  4. 如果使用任何字符变量,则会显示星号(*),但较新的SPSS版本无法处理。

  5. CSV文件以逗号分隔,并且(可以)不使用引号,因此字符串(字符)中不允许使用逗号

  6. 非ASCII字符(例如,变音符号)将导致导致

  7. 如果您的字符包含任何NA值,您将看到...

  1. Newer SPSS versions may show an error that you have too few format definitions in DATA LIST
  2. If there are "labels" for numeric variables stored via attr(), these are lost.
  3. Even if the SPSS vesion supports strings up to 32767, the function write.foreign() stops if there are more than 255 in any variable.
  4. Theres a star (*) if any character variables are used, but newer SPSS versions cannot handle that.
  5. The CSV file is comma-separated and does (can) not use quotes, therefore no commas are allowed in strings (character)
  6. Non-ASCII caracters (e.g. umlauts) will crash the import
  7. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆