简体   繁体   English

在R中读取Spss数据文件

[英]Reading Spss Data file in R

i am using Expss pakage . 我正在使用Expss pakage。 df<-read_spss("test.SAV") I shows the following: df<-read_spss("test.SAV")我显示以下内容:

Warning message: In foreign::read.spss(enc2native(file), use.value.labels = FALSE, : Tally.SAV: Very long string record(s) found (record type 7, subtype 14), each will be imported in consecutive separate variables 警告消息:在外部:: read.spss(enc2native(file),use.value.labels = FALSE,:Tally.SAV:找到了很长的字符串记录(记录类型7,子类型14),每个都将被导入在连续的单独变量中

It shows 4174 Variables in environment Panel.Actual Number of Variables in the Data file around 400. Can anyone among you please help me on this. 它在环境Panel中显示4174个变量。数据文件中的实际变量数约为400。

As mentioned in the comment foreign::read.spss split SPSS long (>255 chars) characters variables into the several columns. 如注释foreign::read.spss将SPSS长(> 255个字符)字符变量拆分为几列。 If the such columns are empty you can drop them without any issues. 如果此类列为空,则可以将其删除而不会出现任何问题。 Convenience function for this: 便利功能:

remove_empty_characters_after_foreign = function(data){
    empty_chars = vapply(data, FUN = function(column) is.character(column) & all(is.na(column)), FUN.VALUE = logical(1))
    additional_chars = grepl("00\\d$", colnames(data), perl = TRUE)
    to_remove = empty_chars & additional_chars
    if(any(to_remove)){
        message(paste0("Removing ", paste(colnames(data)[to_remove], collapse = ", "),"..."))
    }
    data[,!to_remove, drop = FALSE]

}

df = remove_empty_characters_after_foreign(df)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM