Getting error in file(file, “rt”): cannot open the connection

筅森魡賤 提交于 2019-12-02 00:26:25

Most likely you are trying to open files from the working directory instead of the directory in which you called list.files. Instead try

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

Alternatively, you can set the full.names argument to TRUE in list.files to get complete paths:

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

read.csv is looking for the file names in your working directory. By changing your working directory to "C:/Users/Bob/Documents/R/natural-language-processing/class-notes", your code should work just fine.

Code:

setwd("C:/Users/Bob/Documents/R/natural-language-processing/class-notes")

Then re-run your code.

I just spent a lot of time trying to understand what was wrong on my code too...

And it seems to be simple if you are using windows.

When you name your file "blabla.txt" then windows name it "blabla.txt.txt"... That's the same with .CSV files so windows create a file named "001.csv.csv" if you called it "001.csv"

So, when you create your .csv file, just rename it "001" and open it in R using read.table("/absolute/path/of/directory/with/required/001.csv")

It works for me.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!