在文件中获取错误(文件,“ rt”):无法打开连接

an2*_*825 2 r read.table read.csv

我正在运行以下代码...

#Create a list of all the files
file.list <- list.files(path="~/R/natural-language-processing/class-notes", pattern=".csv")

#Loop over file list importing them and binding them together
D1 <- do.call("rbind",lapply(file.list, read.csv, header = TRUE, stringsAsFactors = FALSE))
Run Code Online (Sandbox Code Playgroud)

这是我在do.call上面的行中运行时遇到的错误。

文件错误(文件,“ rt”):无法打开连接

我试过重设我的wd。我目前getwd()

~/R/natural-language-processing
Run Code Online (Sandbox Code Playgroud)

我看过另一个

文件错误(文件,“ rt”):无法打开连接

Ric*_*der 5

很可能您正在尝试从工作目录而不是您在其中调用的目录中打开文件list.files。试一试

D1 <- do.call("rbind",
              lapply(paste0("~/R/natural-language-processing/class-notes/",
                            file.list),
                     read.csv, header = TRUE, stringsAsFactors = FALSE))
Run Code Online (Sandbox Code Playgroud)

另外,您可以将full.names参数设置为TRUEin list.files以获取完整路径:

file.list <- list.files(path="~/R/natural-language-processing/class-notes", 
                        pattern=".csv", full.names = TRUE)
Run Code Online (Sandbox Code Playgroud)

  • 另外,作为一个较小的建议,使用`read.csv`时不需要设置“ header = TRUE”,这是默认设置(与“ read.table”相比)。 (2认同)