Arnaud Mosnier
2015-Jul-26 23:04 UTC
[R] Reading some csv files from different folders and add the name of each files to the first column of files
Hi Lida, You can try this: d<- choose.dir() # choose the folder with the subdirectories containing the csv files f <- list.files(d, full.names = TRUE, recursive = TRUE) # Here the example for the "sing" files selsing <- grep("sing",f) #Select the files notaining the word sing allsing <- data.frame() #Create an empty data frame # Loop among the selected files for (i in 1:length(f[selsing])){ dat <- read.csv(file=f[selsing][i]) # suppose that the csv files have a header allsing <- rbind(allsing,data.frame(FileID gsub("EA_sing_|.txt","",basename(f[selsing][i])), dat)) #Combine the file ID with the other columns and add the result to the all sing object } Now, you just have to do it for the other cases and save your final object. Hope this help ! Arnaud ########################################################################### Date: Sat, 25 Jul 2015 15:03:21 -0500 From: Lida Zeighami <lid.zigh at gmail.com> To: r-help at r-project.org Subject: [R] Reading some csv files from different folders and add the name of each files to the first column of files Message-ID: <CAMqbV1CXN0=R+HxmaYS40sMymGdbhDQK1V+n5bHmBPgBr3M2Yg at mail.gmail.com> Content-Type: text/plain; charset="UTF-8" I have 600 folders in which there are 3 csv files. The name of folders are as follows: EA_aa, EA_bb, EA_cc, EA_dd,.... In each folder there are 3 csv files: in folder EA_aa there are: EA_sing_aa.csvqwerty EA_ska_aa.csv EA_tat_aa.csv In folder EA_bb: EA_sing_bb.csv EA_ska_bb.csv EA_tat_bb.csv ... I need to read all the same kind of files (for example sing files) in one dataframe but before that I should add a column to each files with the name of file as a row names! So in output I should have just 3 csv files such as EA_sing.csv: (rbind all EA_sing_*.csv file) EA_ska.csv: (rbind all EA_ska_*.csv files) EA_tat.csv: (rbind all EA_tat_*.csv files) And in each file the first column should be added as row names which containing the file name! So the format will be as follow: EA_sing.csv: 1st column 2nd column 3rd column aa yhhh ghj aa k ki Fyh bb k ki vgd bb k gki Fyh bb k reci Fyh cc k hcd hyd dd lmb Fyh EA_ska.csv: 1st column 2nd column 3rd column aa yhhh ghj aa k ki Fyh bb k ki Fyh cc k gki Fyh cc k reci oki cc k hcd Fyh dd lmb dsf EA_tat.csv: 1st column 2nd column 3rd column aa yhhh ghj aa k ki Fyh bb k ki Fyh cc k gki Fyh cc k reci oki dd k hcd Fyh dd lmb dsf Would you please help me how to can I do that? Thanks [[alternative HTML version deleted]]
Lida Zeighami
2015-Jul-27 17:53 UTC
[R] Reading some csv files from different folders and add the name of each files to the first column of files
Dear Arnaud, Thank you so much for your reply! It works great! On Sun, Jul 26, 2015 at 6:04 PM, Arnaud Mosnier <a.mosnier at gmail.com> wrote:> Hi Lida, > > You can try this: > > d<- choose.dir() # choose the folder with the subdirectories containing > the csv files > f <- list.files(d, full.names = TRUE, recursive = TRUE) > > # Here the example for the "sing" files > selsing <- grep("sing",f) #Select the files notaining the word sing > > allsing <- data.frame() #Create an empty data frame > > # Loop among the selected files > for (i in 1:length(f[selsing])){ > dat <- read.csv(file=f[selsing][i]) # suppose that the csv files have a > header > allsing <- rbind(allsing,data.frame(FileID > gsub("EA_sing_|.txt","",basename(f[selsing][i])), dat)) #Combine the file > ID with the other columns and add the result to the all sing object > } > > Now, you just have to do it for the other cases and save your final object. > > Hope this help ! > > Arnaud > > ########################################################################### > > Date: Sat, 25 Jul 2015 15:03:21 -0500 > From: Lida Zeighami <lid.zigh at gmail.com> > To: r-help at r-project.org > Subject: [R] Reading some csv files from different folders and add the > name of each files to the first column of files > Message-ID: > <CAMqbV1CXN0> R+HxmaYS40sMymGdbhDQK1V+n5bHmBPgBr3M2Yg at mail.gmail.com> > Content-Type: text/plain; charset="UTF-8" > > > I have 600 folders in which there are 3 csv files. The name of folders are > as follows: EA_aa, EA_bb, EA_cc, EA_dd,.... > > In each folder there are 3 csv files: > in folder EA_aa there are: > EA_sing_aa.csvqwerty > EA_ska_aa.csv > EA_tat_aa.csv > > In folder EA_bb: > EA_sing_bb.csv > EA_ska_bb.csv > EA_tat_bb.csv > ... > > I need to read all the same kind of files (for example sing files) in one > dataframe but before that I should add a column to each files with the name > of file as a row names! > > So in output I should have just 3 csv files such as > EA_sing.csv: (rbind all EA_sing_*.csv file) > EA_ska.csv: (rbind all EA_ska_*.csv files) > EA_tat.csv: (rbind all EA_tat_*.csv files) > > And in each file the first column should be added as row names which > containing the file name! So the format will be as follow: > EA_sing.csv: > 1st column 2nd column 3rd column > aa yhhh ghj > aa k ki Fyh > bb k ki vgd > bb k gki Fyh > bb k reci Fyh > cc k hcd hyd > dd lmb Fyh > > EA_ska.csv: > 1st column 2nd column 3rd column > aa yhhh ghj > aa k ki Fyh > bb k ki Fyh > cc k gki Fyh > cc k reci oki > cc k hcd Fyh > dd lmb dsf > > EA_tat.csv: > 1st column 2nd column 3rd column > aa yhhh ghj > aa k ki Fyh > bb k ki Fyh > cc k gki Fyh > cc k reci oki > dd k hcd Fyh > dd lmb dsf > > Would you please help me how to can I do that? > Thanks >[[alternative HTML version deleted]]