我正在运行以下代码......
#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
时得到的错误。
文件错误(文件,“rt”):无法打开连接
我试过重置我的wd。我现在的getwd()
是
~/R/natural-language-processing
我透过另一个看了看
文件错误(文件,“rt”):无法打开连接
您很可能正在尝试从工作目录而不是您调用list.files
的目录中打开文件。而是试试
D1 <- do.call("rbind",
lapply(paste0("~/R/natural-language-processing/class-notes/",
file.list),
read.csv, header = TRUE, stringsAsFactors = FALSE))
或者,您可以在full.names
中将TRUE
参数设置为list.files
以获取完整路径:
file.list <- list.files(path="~/R/natural-language-processing/class-notes",
pattern=".csv", full.names = TRUE)
read.csv正在工作目录中查找文件名。通过将您的工作目录更改为“C:/ Users / Bob / Documents / R / natural-language-processing / class-notes”,您的代码应该可以正常工作。
码:
setwd("C:/Users/Bob/Documents/R/natural-language-processing/class-notes")
然后重新运行您的代码。
我花了很多时间试图理解我的代码出了什么问题......
如果您使用的是Windows,这似乎很简单。
当您将文件命名为“blabla.txt”时,Windows会将其命名为“blabla.txt.txt”...这与.CSV文件相同,因此如果您将其命名为“001”,则会创建一个名为“001.csv.csv”的文件.CSV”
因此,当您创建.csv文件时,只需将其重命名为“001”并使用read.table(“/ absolute / path / of / directory / with / required / 001.csv”)在R中打开它
这个对我有用。