R 在读取和合并多个文件时添加变量
我想合并几个文件。以下是我的路径:R 在读取和合并多个文件时添加变量,r,merge,path,mutate,R,Merge,Path,Mutate,我想合并几个文件。以下是我的路径: allpaths=c("c:\data\country_GM2\GM_2020_8.csv","c:\data\country_GM2\GM_2020_9.csv","c:\data\country_GM2\GM_2020_10.csv","c:\data\country_GM2\GM_2020_11.csv","c:\data\country_GM2\GM_2020_1
allpaths=c("c:\data\country_GM2\GM_2020_8.csv","c:\data\country_GM2\GM_2020_9.csv","c:\data\country_GM2\GM_2020_10.csv","c:\data\country_GM2\GM_2020_11.csv","c:\data\country_GM2\GM_2020_12.csv")
我写了这样一个函数:
merge_1 = function(filepath) {
datalist= lapply(filepath,
function(x){read.csv(file = x,
header = TRUE,
stringsAsFactors = FALSE)})
Reduce(function(x,y){rbind.fill(x,y)},datalist)
}
data <-merge_1(allpaths)
merge_1=函数(文件路径){
datalist=lappy(文件路径,
函数(x){read.csv(file=x,
header=TRUE,
stringsAsFactors=FALSE)})
Reduce(函数(x,y){rbind.fill(x,y)},数据列表)
}
data您可以在读取数据时添加一个新列,使用regex提取年份和月份值
将功能更改为:
merge_1 = function(filepath) {
datalist= lapply(filepath,function(x) {
transform(read.csv(file = x,header = TRUE, stringsAsFactors = FALSE),
year_month = sub('.*?(\\d+)_(\\d+)\\.csv$', '\\1.\\2', x))
})
Reduce(function(x,y){rbind.fill(x,y)},datalist)
}
data <-merge_1(allpaths)
allpaths=c("C:\\data\\country_GM2\\GM_2020_8.csv",
"C:\\data\\country_GM2\\GM_2020_9.csv")
sub('.*?(\\d+)_(\\d+)\\.csv$', '\\1.\\2', allpaths)
#[1] "2020.8" "2020.9"