R 一次读取多个csv数据并创建新列
我有一个文件,其中有许多R 一次读取多个csv数据并创建新列,r,function,csv,data.table,fread,R,Function,Csv,Data.table,Fread,我有一个文件,其中有许多csv数据。 我想一次读取它们并创建新列,然后合并到一个数据表中。我在这里解释得更多 请看这张图片: 我想根据csv数据标题创建两个新列YEAR和MONTH。 例如,以201508销售报告(伦敦)为例。我想创建YEAR=2015和MONTH=8 我不知道怎么做,但我可以一次阅读它们,而无需创建新列 my_read_data <- function(path){ data <- data.table::fread(path, header = T, str
csv
数据。我想一次读取它们并创建新列,然后合并到一个数据表中。我在这里解释得更多
YEAR
和MONTH
。例如,以
201508销售报告(伦敦)
为例。我想创建YEAR=2015
和MONTH=8
my_read_data <- function(path){
data <- data.table::fread(path, header = T, strip.white = T, fill = T)
data <- data[data[[5]] != 0,]
data <- subset(data, select = c(-1,-7,-10,-12,-13,-14,-15,-17))
}
file.list <- dir(path = "//path/", pattern='\\.csv', full.names = T)
df.list <- lapply(file.list, my_read_data)
dt <- rbindlist(df.list)
然而,我得到了一个错误
Error in data.table::fread(x, header = T, strip.white = T, fill = T) :
File not found: C:\Users\PECHEN\AppData\Local\Temp\RtmpiihFR4\filea0c4d726488
In addition: Warning messages:
1: running command 'C:\Windows\system32\cmd.exe /c (TWM-201508 Sales Report(London).csv) > C:\Users\PECHEN\AppData\Local\Temp\RtmpiihFR4\filea0c4d726488' had status 1
2: In shell(paste("(", input, ") > ", tt, sep = "")) :
'(TWM-201508 Sales Report(London).csv) > C:\Users\PECHEN\AppData\Local\Temp\RtmpiihFR4\filea0c4d726488' execution failed with error code 1
此外,我编辑我的代码:
my_read_data <- function(x){
data <- data.table::fread(x, header = T, strip.white = T, fill = T)
data <- data[data[[5]] != 0,]
data <- subset(data, select = c(-1,-7,-10,-12,-13,-14,-15,-17))
}
file.list <- dir(path = "/path/", pattern='\\.csv', full.names = T)
df.list <- lapply(file.list, my_read_data)
dt <- rbindlist(df.list, idcol = 'id')[, `:=` (YEAR = substr(id,5,8), MONTH = substr(id,9,10))]
my\u read\u data以下是如何将列包含在dplyr
中:
nam <- c("201508 Sales Report(London)", "201509 Sales Report(London)", "201604 Sales Report(London)-Monthly")
dat <- data.frame(file=nam, var=nam)
dat %>%
separate(var, into=c(paste0("parts", 1:5))) %>%
mutate(Year=substring(parts1, 1,4), Month=substring(parts1, 5,6)) %>%
select(Year, Month, file)
# Year Month file
# 1 2015 08 201508 Sales Report(London)
# 2 2015 09 201509 Sales Report(London)
# 3 2016 04 201604 Sales Report(London)-Monthly
nam%
突变(年=子串(第1、1、4部分),月=子串(第1、5、6部分))%>%
选择(年、月、文件)
#年-月档案
#1 2015 08 201508销售报告(伦敦)
#2 2015 09 201509销售报告(伦敦)
#3 2016 04 201604销售报告(伦敦)-每月
在我的评论上展开,假设所有文件都具有相同的结构,下面的操作应该有效:
library(data.table)
# get list of file-names
file.list <- list.files(pattern='*.csv')
# read the files with sapply & fread
# this will create a named list of data.tables
dt.list <- sapply(file.list, fread, simplify=FALSE)
# bind the list together to one data.table
# using the 'idcol'-parameter puts the names of the data.tables in the id-column
# create the YEAR & MONTH variables with 'substr'
DT <- rbindlist(dt.list, idcol = 'id')[, `:=` (YEAR = substr(id,1,4), MONTH = substr(id,5,6))]
使用list.files
读取文件并使用rbindlist
的idcol
-参数()将它们绑定在一起。最后使用DT[,YEAR:=substr(id,1,4)]
获取年份,使用DT[,MONTH:=substr(id,5,6)]
获取月份。您能解释更多吗?我认为这很好,但我不能理解一个更广泛的答案,应该更清楚。您必须首先设置您的工作主管,以便年=substr(id,5,8),月=substr(id,9,10)
可以工作。如果你不设置它,你应该考虑所有的路径,除非你使用的是<代码>使用。如果我想创建一个像我上面的问题my_read_data
这样的函数并使用您的方式,我该怎么做?我得到一个错误,我将编辑我的上面question@PeterChen很抱歉这么晚才回复,您仍然有这个问题吗?@PeterChen通过使用simplify=FALSE
,数据表的列表变成了一个命名列表(以文件名作为名称)simplify=TRUE
是默认值,将创建一个未命名列表。将rbindlist
应用于未命名列表时,id
列只获取数字。将rbindlist
应用于命名列表(即使用simplify=FALSE
的结果),id
列获取相应文件的名称。有关阅读文件列表的更广泛的答案。
library(data.table)
# get list of file-names
file.list <- list.files(pattern='*.csv')
# read the files with sapply & fread
# this will create a named list of data.tables
dt.list <- sapply(file.list, fread, simplify=FALSE)
# bind the list together to one data.table
# using the 'idcol'-parameter puts the names of the data.tables in the id-column
# create the YEAR & MONTH variables with 'substr'
DT <- rbindlist(dt.list, idcol = 'id')[, `:=` (YEAR = substr(id,1,4), MONTH = substr(id,5,6))]
dt.list <- sapply(file.list, fread, drop = c(1,7,10,12:15,17), simplify=FALSE)