[R] Creating data frame from individual files
arun
smartpink111 at yahoo.com
Thu Apr 4 03:42:50 CEST 2013
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.
More information about the R-help
mailing list