[R] problem with previous code

arun smartpink111 at yahoo.com
Thu Mar 27 11:55:09 CET 2014


HI Elio,

May be this helps:
 indx <- order(gsub("\\d+","",colnames(res)))
res1 <- res[indx,indx]
A.K.







On Thursday, March 27, 2014 6:24 AM, Elio Shijaku <selius at gmail.com> wrote:

Hi Arun,

Thanks a lot for your continuous help. I am attaching a sample with the row / column names. Basically, the order should be alphabetical because then I don´t need to rearrange the data in Excel and since I have many columns of data, the alphabetical order makes dealing with specific values easier since I can just copy the row where different variable values for the same name are located.

Does this help answering your question?

Best,

E.







>On Tuesday, March 25, 2014 3:57 PM, Elio Shijaku <selius at gmail.com> wrote:
>
>Hi Arun,
>
>Thanks a lot, it worked, one small question, is there any code to arrange the variables in each matrix in alphabetical order?
>
>Have a safe flight!
>
>
>E.
>
>
>
>
>On Tue, Mar 25, 2014 at 6:51 PM, arun <smartpink111 at yahoo.com> wrote:
>
>HI Elio,
>>I am at the airport.  So, probably, I may not be able to access the emails that frequently.
>>
>>Arun
>>
>>
>>
>>On Tuesday, March 25, 2014 1:47 PM, arun <smartpink111 at yahoo.com> wrote:
>>Hi Elio,
>>I gy.
>>
>>
>>uess 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