Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading Multiple CSV files as data frames in R

Tags:

r

csv

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

like image 430
Andrea Ovalle Avatar asked Oct 02 '17 02:10

Andrea Ovalle


People also ask

How are multiple datasets imported and combined simultaneously in R?

files(), lapply(), and bind_rows() from these packages and pass the required parameters to these functions to merge the given multiple CSV files to a single data frame in the R programming language.

How do I read a list of files in R?

To list all files in a directory in R programming language we use list. files(). This function produces a list containing the names of files in the named directory. It returns a character vector containing the names of the files in the specified directories.


1 Answers

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)
    }


********  ********  ********  ********  ********  ********  ********  ********  ********  ********
like image 72
ASH Avatar answered Nov 15 '22 05:11

ASH