[英]Reading Multiple CSV files as data frames in R
我一直在竭盡全力想弄清楚這個問題,我已經閱讀了很多博客並嘗試了不同的方法,但我仍然遇到同樣的錯誤,而且我不明白我的代碼有什么問題。我正在嘗試拉幾個csv 文件一次全部放入 R,我不斷收到以下錯誤。
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)))
}`
所有 CSV 文件都位於 Tea_Party_ONE_Lumicycle_data 中。
謝謝你的幫助
有很多方法!
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)
}
******** ******** ******** ******** ******** ******** ******** ******** ******** ********
正如評論中提到的那樣,問題在於paste
正在添加空格分隔符。 您可以使用paste0
,也可以只獲取完整的路徑名。
library('tidyverse')
files <- list.files(path = Tea_ONE, pattern = '.csv$', full.names = T) %>%
map(read_csv, skip = 1)
library(purrr)
csv_files = list.files(path = 'data/folder/', pattern = "csv$", full.names = TRUE)
data_stacked <- map_dfr(csv_files, read_csv)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.