[R] Combining many csv files into one and adding a column with an id of each csv file read

jim holtman jholtman at gmail.com
Wed Apr 9 01:12:29 CEST 2008


Here is one way of doing it.  Read the data into a list and then use
'do.call(rbind...':

filenames <- Sys.glob("*.csv")  # however you get the list of file
allData <- lapply(filenames, function(.file){
    dat<-read.csv(filename, header=F)
    dat<-dat[c(-1:-3),c(-1,-4,-5,-6,-7,-9,-10,-11,-12)]
    names(dat)<-c('time','T1','T2')
    dat <- dat[,1:3]

    dat$id<-as.character(filename)
    dat    # return the dataframe
})
# combine into a single dataframe
myDF <- do.call(rbind, allData)

On Tue, Apr 8, 2008 at 3:11 PM, Judith Flores <juryef at yahoo.com> wrote:
> Dear R experts,
>
>   I have been looking into the help-pages and old
> questions from the R-Help site, but the options
> offered there don't seem to work in my case.
>
>  First of all, I am working on Windows XP, using R
> version 2.6.2.
>
>  I am attaching two csv files as an example of how
> the data I am traying to put together is delivered to
> us. On the first row of every csv file is the name of
> the file, along with the pathfile. This is what I have
> been doing with every csv files (per seprate), which
> of course is not the most efficient way to do it;
> basically, it reads the csv file, then deletes the
> first 3 rows and some columns that we don't need and
> finally I add another column that identifies all the
> rows of the csv file as belonging to one subject only,
> I have to do this for further manipulations with all
> the data:
>
> filename<-'1_504_d0.csv'
>
> dat<-read.csv(filename, header=F)
> dat<-dat[c(-1:-3),c(-1,-4,-5,-6,-7,-9,-10,-11,-12)]
> names(dat)<-c('time','T1','T2')
> dat<-dat[,1:3]
>
> dat$id<-as.character(filename)
>
>  Since I have multiple csv files to read and I need
> to have them consolidated in one data frame, how can I
> apply the above manipulations to all the csv files and
> at the same time put them in one files?
>
> At the end, I need a big data frame that has 4
> columns"
>
> 'Time', 'T1', T2', 'id'
>
> I hope I have provided enough information.
>
> Thank you in advance for your help,
>
> Judith
>
>
>
>      ____________________________________________________________________________________
> [[elided Yahoo spam]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?



More information about the R-help mailing list