[R] Creating data frame from individual files

arun smartpink111 at yahoo.com
Thu Apr 4 21:24:53 CEST 2013


HI,

May be this helps:

lst1<- structure(list(File1 = structure(list(ColA = c("A", "B"), ColB = c(2.4, 
2.1)), .Names = c("ColA", "ColB"), class = "data.frame", row.names = c(NA, 
-2L)), File2 = structure(list(colA = c("A", "B"), ColB = c(1.3, 
0.4)), .Names = c("colA", "ColB"), class = "data.frame", row.names = c(NA, 
-2L))), .Names = c("File1", "File2"))

res<-do.call(cbind,lapply(lst1,`[`,2))
row.names(res)<- unlist(unique(sapply(lst1,`[`,1)))
 colnames(res)<- names(lst1)
 res
#  File1 File2
#A   2.4   1.3
#B   2.1   0.4
A.K.


________________________________
 From: Adrian Johnson <oriolebaltimore at gmail.com>
To: arun <smartpink111 at yahoo.com> 
Sent: Thursday, April 4, 2013 2:49 PM
Subject: Re: [R] Creating data frame from individual files
 

HI:
sorry for confusion. 
I have 72 files and all 72 files have first identical column. 2nd column format is same but different numbers.
every file in 72 files have identical number of rows. (90,799).



ColA   ColB
A        2.4
B        2.1


File 2:

colA   ColB

A   1.3
B   0.4


I want to have one data frame with the following:
         File1   File 2 
A        2.4      1.3
B        2.1       0.4


Finally I will have a data frame of 90,799 x 72.

Thank you for helping.
Adrian




On Wed, Apr 3, 2013 at 9:19 PM, arun <smartpink111 at yahoo.com> wrote:

Hi,
>Sorry, I didnt understand your question.  You have 72 files, among those 70 files have identical column 1.. Each file has 90,799 rows.  But you wanted to create matrix with 40 rows X 70 columns.  What are those 40 rows and 70 columns?  Is it the first 40 rows and the identical 70 columns?
>
>
>
>
>
>----- Original Message -----
>From: Adrian Johnson <oriolebaltimore at gmail.com>
>To: r-help <r-help at r-project.org>
>Cc:
>Sent: Wednesday, April 3, 2013 7:05 PM
>Subject: [R] Creating data frame from individual files
>
>Dear Group:
>I have 72 files (.txt).
>
>Each file has 2 columns and column 1 is always identical for all 70 files.
>Each file has 90,799 rows and is standard across all files.
>
>I want to create a matrix 40(rows) x 70 columns.
>
>I tried :
>
>temp = list.files(pattern="*.txt")
>named.list <- lapply(temp, read.delim)
>library(data.table)
>files.matrix <-rbindlist(named.list)
>
>> dim(files.matrix)
>[1] 6537456       2
>
>What happened here is all 90K rows for 72 files were rbinded.
>
>I want to cbind.
>
>Could anyone please help me.
>Thanks
>Adrian
>
>    [[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.
>
>



More information about the R-help mailing list