Reputation: 73
I have been pulling my hair out trying to figure this out, I have read so many blogs and tried different things but I still get the same error, and I don't understand what is wrong with my code.I am trying to pull several csv files into R all at once and I keep getting the following error.
No such file or directoryError in file(file, "rt") : cannot open the connection
`Tea_ONE <- "~/Desktop/Circadian Rhythms
Sem/Project/Tea_Party_ONE/Tea_Party_ONE_Lumicycle_data/"
files <- list.files(path = Tea_ONE, pattern = ".csv$")
for(i in 1:length(files)){
assign(files[i],
read.csv(paste(Tea_ONE, files[i], header = T, skip = 1)))
}`
all of the CSV files are located in the Tea_Party_ONE_Lumicycle_data.
Thanks for any help
Upvotes: 6
Views: 23158
Reputation: 518
library(purrr)
csv_files = list.files(path = 'data/folder/', pattern = "csv$", full.names = TRUE)
data_stacked <- map_dfr(csv_files, read_csv)
Upvotes: 2
Reputation: 20352
There are so many ways!!
setwd("C:/your_path_here")
fnames <- list.files()
csv <- lapply(fnames, read.csv)
result <- do.call(rbind, csv)
******** ******** ******** ******** ******** ******** ******** ******** ******** ********
filedir <- setwd("C:/your_path_here")
file_names <- dir(filedir)
your_data_frame <- do.call(rbind,lapply(file_names,read.csv))
******** ******** ******** ******** ******** ******** ******** ******** ******** ********
filedir <- setwd("C:/your_path_here")
file_names <- dir(filedir)
your_data_frame <- do.call(rbind, lapply(file_names, read.csv, skip = 1, header = FALSE))
******** ******** ******** ******** ******** ******** ******** ******** ******** ********
filedir <- setwd("C:/your_path_here")
file_names <- dir(filedir)
your_data_frame <- do.call(rbind, lapply(file_names, read.csv, header = FALSE))
******** ******** ******** ******** ******** ******** ******** ******** ******** ********
#
temp <- setwd("C:/your_path_here")
temp = list.files(pattern="*.csv")
myfiles = lapply(temp, read.delim)
******** ******** ******** ******** ******** ******** ******** ******** ******** ********
# Here is another options to convert the .csv files into one data.frame. Using R base functions.
# This is order of magnitude slower than the options below.
files <- setwd("C:/your_path_here")
# Get the files names
files = list.files(pattern="*.csv")
# First apply read.csv, then rbind
myfiles = do.call(rbind, lapply(files, function(x) read.csv(x, stringsAsFactors = FALSE)))
library(readr)
library(dplyr)
tbl = lapply(files, read_csv) %>% bind_rows()
******** ******** ******** ******** ******** ******** ******** ******** ******** ********
# LIST OF FILE PATHS
library(readr)
library(stringr)
List_of_file_paths <- list.files(path ="C:/your_path_here/", pattern = ".csv", all.files = TRUE, full.names = TRUE)
******** ******** ******** ******** ******** ******** ******** ******** ******** ********
# LIST OF FILES IN FOLDER
xlist<-list.files(pattern = "*.csv")
for(i in xlist) {
x <- read.csv((i))
assign(i, x)
}
******** ******** ******** ******** ******** ******** ******** ******** ******** ********
Upvotes: 18
Reputation: 9107
As was mentioned in the comments, the problem is that paste
is adding a space separator. You can use paste0
or you can just get the full path names.
library('tidyverse')
files <- list.files(path = Tea_ONE, pattern = '.csv$', full.names = T) %>%
map(read_csv, skip = 1)
Upvotes: 7