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]]
Hi,
suppose, you have 3 files with 2 columns:
named.list<- list(structure(list(col1 = 1:6, col2 = c(0.5, 0.2, 0.3, 0.3,
0.1, 0.2)), .Names = c("col1", "col2"), class =
"data.frame", row.names = c(NA,
-6L)), structure(list(col1 = 1:6, col2 = c(0.9, 0.7, 0.5, 0.2,
0.5, 0.2)), .Names = c("col1", "col2"), class =
"data.frame", row.names = c(NA,
-6L)), structure(list(col1 = 7:12, col2 = c(0.1, 0.5, 0.9, 0.3,
0.6, 0.4)), .Names = c("col1", "col2"), class =
"data.frame", row.names = c(NA,
-6L)))
?named.list1<-do.call(cbind,named.list)
?mat1<-as.matrix(named.list1[!duplicated(as.list(named.list1))])
?dimnames(mat1)<-NULL
?mat1
#???? [,1] [,2] [,3] [,4] [,5]
#[1,]??? 1? 0.5? 0.9??? 7? 0.1
#[2,]??? 2? 0.2? 0.7??? 8? 0.5
#[3,]??? 3? 0.3? 0.5??? 9? 0.9
#[4,]??? 4? 0.3? 0.2?? 10? 0.3
#[5,]??? 5? 0.1? 0.5?? 11? 0.6
#[6,]??? 6? 0.2? 0.2?? 12? 0.4
Because you mentioned 72 files and you need 70 columns in the result matrix, I
think you need only the first column.
In that case:
?named.list2<-do.call(cbind,lapply(named.list,`[`,1))
mat2<- as.matrix(named.list2[!duplicated(as.list(named.list2))])
?dimnames(mat2)<-NULL
?mat2
#???? [,1] [,2]
#[1,]??? 1??? 7
#[2,]??? 2??? 8
#[3,]??? 3??? 9
#[4,]??? 4?? 10
#[5,]??? 5?? 11
#[6,]??? 6?? 12
I am not sure this is what you wanted.
A.K.
----- 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.
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.
>
>