Re: [R] merging data list in to single data frame

From: Uwe Ligges <ligges_at_statistik.tu-dortmund.de>
Date: Mon, 04 Apr 2011 18:21:52 +0200

On 04.04.2011 16:41, Umesh Rosyara wrote:
> Dear R community members
>
>
>
> I did find a good way to merge my 200 text data files in to a single data
> file with one column added will show indicator for that file.
>
>
>
> filelist = list.files(pattern = "K*cd.txt")

I doubt you meant "K*cd.txt" but "^K[[:digit:]]*cd\\.txt$".

# the file names are K1cd.txt
> .................to K200cd.txt
>
> data_list<-lapply(filelist, read.table, header=T, comment=";", fill=T)

Replace by:

data_list <- lapply(filelist, function(x)

    cbind(Filename = x, read.table(x, header=T, comment=";", fill=TRUE))

And then:

result <- do.call("rbind", data_list)

Uwe Ligges

>
>
>
> This will create list, but this is not what I want.
>
>
>
> I want a single dataframe (all separate dataframes have same variable
> headings) with additional row for example
>
>
>
> ; just for example, two small datasets are created by my component datasets
> are huge, need automation
>
> ;read from file K1cd.txt
>
> var1 var2 var3 var4
>
> 1 6 0.3 8
>
> 3 4 0.4 9
>
> 2 3 0.4 6
>
> 1 0.4 0.9 3
>
>
>
> ;read from file K2cd.txt
>
> var1 var2 var3 var4
>
> 1 16 0.6 7
>
> 3 14 0.4 6
>
> 2 1 3 0.4 5
>
> 1 0.6 0.9 2
>
>
>
> the output dataframe should look like
>
>
>
> Fileno var1 var2 var3 var4
>
> 1 1 6 0.3 8
>
> 1 3 4 0.4 9
>
> 1 2 3 0.4 6
>
> 1 1 0.4 0.9 3
>
> 2 1 16 0.6 7
>
> 2 3 14 0.4 6
>
> 2 2 1 3 0.4 5
>
> 2 1 0.6 0.9 2
>
>
>
> Please note that new file no column is added
>
>
>
> Thank you for the help.
>
>
>
> Umesh R
>
>
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.



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. Received on Mon 04 Apr 2011 - 16:25:32 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 04 Apr 2011 - 17:10:28 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive