[R] problem with previous code

arun smartpink111 at yahoo.com
Tue Mar 25 18:47:37 CET 2014


Hi Elio,
I guess the error is here:
    

as.vector(sapply(lst2,rownames)) #still a list

length(unique(as.vector(sapply(lst2,rownames))) )# length of list

#22


uNrownames <- unique(unlist(sapply(lst2,rownames)))
 length(uNrownames)
#[1] 90

res <- matrix(0,nrow=length(uNrownames),ncol=length(uNrownames),dimnames=list(uNrownames,uNrownames))
for(i in seq_along(lst2)){
 res[rownames(lst2[[i]]),rownames(lst2[[i]])] <- res[rownames(lst2[[i]]),rownames(lst2[[i]])]+lst2[[i]]
 res
 }
 dim(res)
#[1] 90 90
 res[1:3,1:3]
#     a15 a187 a200
#a15    0    1    1
#a187   1    0    0
#a200   1    0    0

A.K.


On Tuesday, March 25, 2014 12:50 PM, Elio Shijaku <selius at gmail.com> wrote:

Hi Arun,

Sorry to disturb once again but I have encountered an error while trying to combine 22 symmetric matrices of different dimensions (in the attached file) into one single symmetric matrix.

Here is what I get as an error:


> length(list.files(pattern=".txt")) [1] 22 > lst1 <- lapply(list.files(pattern=".txt"),function(x) + read.table(x,header=TRUE,stringsAsFactors=FALSE)) > sapply(lst1,dim)  [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15]
[1,]   65   68   63   72   70   72   73   78   77    77    75    80    77    75    76
[2,]   65   68   63   72   70   72   73   78   77    77    75    80    77    75    76 [,16] [,17] [,18] [,19] [,20] [,21] [,22]
[1,]    77    69    73    66    62    59    56
[2,]    77    69    73    66    62    59    56 > sapply(lst1,function(x) all(apply(rbind(colnames(x),rownames(x)),2,function(y) y[1]==y[2])))  [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[17] TRUE TRUE TRUE TRUE TRUE TRUE > lst2 <- lapply(lst1,as.matrix) > uNrownames <- unique(as.vector(sapply(lst2,rownames))) > res <- matrix(0,nrow=length(uNrownames), ncol=length(uNrownames), dimnames=list(uNrownames,uNrownames)) > for(i in seq_along(lst2)){ + res[rownames(lst2[[i]]),rownames(lst2[[i]])] <- res[rownames(lst2[[i]]),rownames(lst2[[i]])] + lst2[[i]] + res + } Error in res[rownames(lst2[[i]]), rownames(lst2[[i]])] :  subscript out of bounds


Any idea about what may cause the error?

Thanks a lot.

Best,

Elio




More information about the R-help mailing list