简体   繁体   English

在R中一次读取许多SPSS文件

[英]Read many SPSS files at once in R

I am trying to read all SPSS files in a folder in R to different data.frames. 我正在尝试将R文件夹中的所有SPSS文件读取到不同的data.frames。 I only need that, all of those files read into R dataframes. 我只需要所有这些文件读入R数据帧。

I believe I need a for loop or maybe some solution with lapply or sapply. 我相信我需要一个for循环,或者可能需要lapply或sapply解决方案。 I have tried many options with loops or lapply, but the best I got was to create all the data. 我已经尝试了很多使用循环或lapply的选项,但是我得到的最好的办法就是创建所有数据。

The best I got so far was with the following code (adapted from a solution to open multiple CSV files): 到目前为止,我得到的最好的结果是以下代码(适用于打开多个CSV文件的解决方案):

library(foreign)

filenames <- list.files()

filelist <- lapply(filenames, read.spss, to.data.frame = TRUE)

names(filelist) <- c("nic", "hon", "chi", "cr", "repdom", "ecu", "col", 
"sal", "par", "mex", "arg", "gua", "pan", "uru", "bra")

invisible(lapply(names(filelist), function(x)  
assign(x,filelist[[x]],envir=.GlobalEnv)))

With this code, I got all the files read, but as lists, not as data.frames. 使用此代码,我读取了所有文件,但以列表的形式读取,而不是以data.frames的形式读取。 I don't know where I could put a "as.data.frame" or something like this to get what I want. 我不知道在哪里可以放置“ as.data.frame”或类似的东西来得到我想要的东西。

Maybe a solution would be to loop for all the files in the folder, so that the first element in the list (the first file), would be read as df.1, then df.2, etc. It doesn't matter what the names of the dataframes will be, I just need a simple code to read all of them into dataframes. 可能的解决方案是循环访问文件夹中的所有文件,以便将列表中的第一个元素(第一个文件)读取为df.1,然后读取为df.2,依此类推。数据帧的名称将是,我只需要一个简单的代码即可将它们全部读入数据帧。

At present, your object file_list is probably a character vector, not a list. 目前,您的对象file_list可能是一个字符向量,而不是列表。 If you prefer the resulting object to be a list, try: 如果您希望将结果对象作为列表,请尝试:

library(foreign)

file_list <- as.list(list.files())
res <- lapply(file_list, read.spss, to.data.frame = TRUE)

This generates a vector of file names and coerces that vector to a list class object. 这将生成一个文件名向量,并将其强制转换为列表类对象。 Then, we apply read.spss to the list of file names using lapply , and assign the results to the object res . 然后,我们应用read.spss使用的文件名列表lapply ,并分配结果的对象res

Alternatively, you can apply read.spss straight to the character vector object (without coercing the vector to a list) using sapply : 另外,您也可以申请read.spss直奔字符矢量对象(不强迫的载体列表)使用sapply

file_vector <- list.files()
res <- sapply(file_vector, read.spss, to.data.frame = TRUE)

See also: Read multiple CSV files into separate data frames 另请参阅:将多个CSV文件读取到单独的数据框中

It finally worked. 终于成功了。 The same code as I've put in the update of the question, but with the slight difference of using haven instead of foreign : 与我在问题的更新中输入的代码相同,但是使用避风港而不是外国人稍有不同:

library(haven) 

filenames <- list.files()

filelist <- lapply(filenames, read_spss)

names(filelist) <- c("nic", "hon", "chi", "cr", "repdom", "ecu", "col", 
"sal", "par", "mex", "arg", "gua", "pan", "uru", "bra")

invisible(lapply(names(filelist), function(x) 
assign(x,filelist[[x]],envir=.GlobalEnv)))

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

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