Hello I am attempting to create a single dataframe with multiple .csv files from a folder that will be updating/added to.
I have found previous answers on here however I am having a rather simple error
Error in read.table(file = file, header = header, sep = sep, quote = quote, : 'file' must be a character string or connection
dir<-"asdfasdfasdfasf/asdfasdfs" #change this to your directory
temp <- list.files(pattern="*.csv")
importDM<-lapply(temp, read.csv)
rawDM<-read.csv(importDM, header = TRUE) #will read csv
Please let me know what I am doing wrong!
Cheers,
答案 0 :(得分:1)
从我在list.files函数的调用中看到的,您应该添加path参数并指定&#34; dir&#34;它。 变量importDM是一个列表,因为你已经在lapply中调用了read.csv,所以不需要rawDM变量。 如果文件具有相同的数据结构,则可以使用as.data.frame将importDM转换为数据框。如果列表元素不遵循相同的结构,那么它就会更加有效。
这是三个csv文件的工作示例。
dir <-"./data" #change this to your directory
temp <- list.files(pattern="*.csv", path = dir, full.names = TRUE)
importDM<-lapply(temp, read.csv, header = FALSE)
df <- as.data.frame(importDM)
每个csv文件包含三个数字(1,2,3 - 4,5,6 - 7,8,9)
答案 1 :(得分:0)
试试这个:
temp <- list.files(pattern="*.csv")
dataset <- do.call(rbind,lapply(temp,
read.csv,header=TRUE))
答案 2 :(得分:0)
这应该这样做 -
dir<-"asdfasdfasdfasf/asdfasdfs" #change this to your directory
temp <- list.files(pattern="*.csv")
importDM<-lapply(temp, read.csv)
rawDM <- do.call(rbind, lapply(raw, read.csv, header = TRUE))