2 回答

TA貢獻1898條經驗 獲得超8個贊
使用R您可以首先搜索一個文件夾中包含的所有 csv,然后sapply對該向量進行處理(使用dplyr包來執(zhí)行所需的操作)。最后,在 中指示的同一文件夾中搜索結果文件list.files。
library(dplyr)
#Find all the csv files in the indicated path
#Change the path location to the folder where you have your csv files
file_locs<-list.files(path="C:/Folder with csvs",
pattern = ".csv",
full.names = T)
sapply(file_locs, function(x){
#Read csv, skipping first line if it contains the A, b, c entries
#as headers, if not you can remove the "skip = 1"
df<-read.csv(x, skip = 1)
#Use dplyr to get the Value sum, grouped by Name
resuls<-df %>%
group_by(Name) %>%
summarize(sumVal = sum(Value))
#Get the csv original name, i.e., without the .csv part
file_name<-strsplit(x,".csv")[[1]][1]
#Write the results using the original file name and adding: _resul
write.csv(resuls, paste0(file_name,"_resul.csv"),row.names = F)
})

TA貢獻1844條經驗 獲得超8個贊
1.創(chuàng)建可重現的最小示例數據
df <- data.frame(A=rep(c("Jack", "Joe"), 3), C=runif(6))
2.使用dplyr
庫的解決方案:
library(dplyr)summarised <- df %>% group_by(A) %>% summarise(Total = sum(C))write.csv(summarised, "File_Name.csv")
添加回答
舉報