如何使用r自动化子集化多个文件 [英] How to automate subsetting multiple files using r

查看:191
本文介绍了如何使用r自动化子集化多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨家伙是新的R和我很舒服创建子集,如果我一次处理一个文件....但我有麻烦自动化到多个文件...所以在我的情况下,我想自动化子集化存在于给定文件夹的多个子文件夹中的多个csv文件的过程...我想创建多个子集文件,包括每个文件的100行,并将它们写入新文件和子集化文件的名称应该与它们被子集化的文件的相同...任何帮助赞赏...感谢!!!

Hi guys am new to R and I am comfortable with creating subsets if i handle one file at a time .... But I am having trouble automating that to multiple files...So in my case,I want to automate the process of subsetting multiple csv files which are present in multiple subfolders of a given folder ...I want to create multiple subset files which include say the the 100 rows of each file and write them into new files and the name of the subsetted files should be same as that of the file from which they were subsetted... Any help appreciated... Thanks!!!

推荐答案

我在我的文件夹 Temp 中创建了一些子文件夹。如果工作目录为 Temp 。假设每个数据集中的行数> = 100

I created a couple of subfolders in my folder Temp. If the working directory is Temp. Assuming that the number of rows in each dataset is >= 100

files <- list.files(recursive=TRUE, full.names=TRUE)
files
#[1] "./Temp1/file1.csv"   "./Temp2/file2_2.csv" "./Temp2/file2.csv" 

lst1 <- lapply(files, function(x) read.csv(x, sep='')[1:100,])
Pref <- sub("/[^/]+$", '', files)

然后将文件与 old 文件一起写入相应的文件夹。

The subset files are then written to the corresponding folders along with the old file.

invisible(lapply(seq_along(lst1), function(i) 
            write.csv(lst1[[i]],paste(Pref[i],paste0('Subset',
           basename(files[i])), sep="/"), quote=FALSE, row.names=FALSE)))

list.files(recursive=TRUE, full.names=TRUE)
#[1] "./Temp1/file1.csv"         "./Temp1/Subsetfile1.csv"  
#[3] "./Temp2/file2_2.csv"       "./Temp2/file2.csv"        
#[5] "./Temp2/Subsetfile2_2.csv" "./Temp2/Subsetfile2.csv"  

这篇关于如何使用r自动化子集化多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆