【问题标题】:Reading Multiple CSV files as data frames in R在 R 中读取多个 CSV 文件作为数据框
【发布时间】:2017-10-02 02:29:58
【问题描述】:

我一直在努力解决这个问题,我阅读了很多博客并尝试了不同的方法,但我仍然遇到同样的错误,而且我不明白我的代码有什么问题。我正在尝试一次将多个 csv 文件全部拉入 R 中,我不断收到以下错误。

No such file or directoryError in file(file, "rt") : 无法打开连接

`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 中。

感谢您的帮助

【问题讨论】:

  • @RonakShah 请注意,OP 将完整路径粘贴在一起。问题实际上可能是默认的 paste() 设置在路径中添加了一个空格。不是错误地将 read.csv 参数传递给 paste() 的原因。
  • @joran 是的。我错过了。谢谢。你是对的,一定是因为paste 参数中默认添加了一个额外的空间。 OP 应该尝试paste0

标签: r csv


【解决方案1】:

方法太多了!!

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


********  ********  ********  ********  ********  ********  ********  ********  ********  ********

【讨论】:

    【解决方案2】:

    正如 cmets 中提到的,问题在于 paste 添加了一个空格分隔符。您可以使用paste0,也可以只获取完整路径名。

    library('tidyverse')
    
    files <- list.files(path = Tea_ONE, pattern = '.csv$', full.names = T) %>%
      map(read_csv, skip = 1)
    

    【讨论】:

    • full.names = TRUE 参数很棒 - 所以你不需要改变你的工作目录!
    【解决方案3】:

    purrr 包使用 map_dfr 函数

    library(purrr) 
    

    列出目录中所有以csv结尾的文件

    csv_files = list.files(path = 'data/folder/', pattern = "csv$", full.names = TRUE)
    

    读取csv文件并放入dataframe

    data_stacked <- map_dfr(csv_files, read_csv)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-24
      • 1970-01-01
      • 2021-10-14
      • 1970-01-01
      • 2017-07-25
      • 1970-01-01
      • 2016-06-14
      • 1970-01-01
      相关资源
      最近更新 更多