r - 在文件中获取错误(文件, "rt"): cannot open the connection

标签 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))

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

Error in file(file, "rt") : cannot open the connection

我试过重置我的 wd。我当前的 getwd()

~/R/natural-language-processing

我看过其他的

Error in file(file, “rt”): cannot open connection

最佳答案

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

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

或者,您可以在 list.files 中将 full.names 参数设置为 TRUE 以获取完整路径:

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

关于r - 在文件中获取错误(文件, "rt"): cannot open the connection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49090622/

相关文章:

r - 在函数中使用带有 read.csv 的粘贴

R 的 read.csv() 省略行

r - 由于 stata 导入的标签, is.pbalanced.default(x) : argument "y" is missing, 中出现错误,没有默认值

r - 是否可以/建议跳过 roxygen 以支持 roxygen2?

R:如何获取矩阵真元素的行名和列名?

R:在读取数据帧时识别列数

r - 将原始数据导入 R

r - 当数据未格式化为表格时将数据导入 R 中

r - 导出时,如何用单个空格替换read.csv()生成的列名中的 “.”?

r - sparklyr 更改所有列名 spark dataframe