Re: [R] problem with previous code

2014-03-27 Thread Elio Shijaku
Excellent Arun, it worked, many thanks.

E.


On Thu, Mar 27, 2014 at 11:55 AM, arun  wrote:

> 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 
> 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 
> 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  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  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
> >>#a15011
> >>#a187   100
> >>#a200   100
> >>
> >>A.K.
> >>
> >>
> >>
> >>On Tuesday, March 25, 2014 12:50 PM, Elio Shijaku 
> 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
> 77777580777576
> >>[2,]   65   68   63   72   70   72   73   78
> 77777580777576 [,16] [,17] [,18] [,19] [,20] [,21]
> [,22]
> >>[1,]77697366625956
> >>[2,]77697366625956 > 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
> >>
> >
>

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] problem with previous code

2014-03-27 Thread arun
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  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  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  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  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  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   7777758077
>>7576
>>[2,]   65   68   63   72   70   72   73   78   7777758077
>>7576 [,16] [,17] [,18] [,19] [,20] [,21] [,22]
>>[1,]77697366625956
>>[2,]77697366625956 > 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
>>
>

__
R-help@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.


Re: [R] problem with previous code

2014-03-25 Thread arun
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  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   777775807775 
   76
[2,]   65   68   63   72   70   72   73   78   777775807775 
   76 [,16] [,17] [,18] [,19] [,20] [,21] [,22]
[1,]77697366625956
[2,]77697366625956 > 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

__
R-help@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.


Re: [R] problem with previous code

2014-03-05 Thread Elio Shijaku
Hi Arun,

Yes, that last command m1[indx2N] <- m2[sort(indx1)] did exactly the trick,
now the variable and their values are perfectly matched. Thanks a lot for
your great help.

Best,

Elio


On Wed, Mar 5, 2014 at 1:17 AM, arun  wrote:

>
>
> Hi Elio,
>
> If you change the last line of the code:
>
> m1[indx2N] <- m2[indx1]
>
> to
> m1[indx2N] <- m2[sort(indx1)]
>
>  sum(m1[rownames(m1)=="p79",])
> #[1] 191
>  sum(m2[rownames(m2)=="p79",])
> #[1] 191
>
>
> The problem is in the order of the colnames/rownames in each of the
> datasets.  I tested it on something like:
> m1 <- matrix(0,12,12,dimnames=rep(list(paste0("a",1:12)),2))
>  set.seed(49)
>  m2 <-
> matrix(sample(0:2,4*4,replace=TRUE),ncol=4,dimnames=rep(list(c("a4","a6","a9","a12")),2))
>
> which didn't show the problem.
>
>
> But, if I change the order of colnames:
>
> m1 <- matrix(0,12,12,dimnames=rep(list(paste0("a",c(1:3,8:12,4:7))),2))
>
> set.seed(49)
>  m2 <-
> matrix(sample(0:2,4*4,replace=TRUE),ncol=4,dimnames=rep(list(c("a9","a4","a6","a12")),2))
> vec1 <- paste0(rownames(m1)[row(m1)],colnames(m1)[col(m1)])
> vec2 <- paste0(rownames(m2)[row(m2)],colnames(m2)[col(m2)])
> indx <- match(vec1,vec2)
> indx1 <- indx[!is.na(indx)]
>
> indx2 <- match(vec2,vec1)
> indx2N <- indx2[!is.na(indx2)]
> m1[indx2N] <- m2[sort(indx1)]
> m1
>
>
> Hope this helps.
> A.K.
>
>
>
>
>
>
> On Tuesday, March 4, 2014 3:16 PM, Elio Shijaku  wrote:
>
> Hi Arun,
>
> Sorry to disturb, but while trying your code to combine two matrices of
> unequal dimensions, I noticed a problem, the sum for each variable does not
> match, but the overall variables sum does match for both matrices,
> something isn't going right, perhaps the variables are getting mixed up,
> any idea??
>
> Here is the code you sent me and the zipped files for testing:
>
>
> dat1 <-
> read.table("mtest.txt",header=TRUE)
> dim(dat1)
>
> dat2 <-
> read.table("1998res_x.txt",header=TRUE)
> dim(dat2)
> m1 <- as.matrix(dat1)
> m2 <- as.matrix(dat2)
> vec1 <-
> paste0(rownames(m1)[row(m1)],colnames(m1)[col(m1)])
> vec2 <- paste0(rownames(m2)[row(m2)],colnames(m2)[col(m2)])
> indx <- match(vec1,vec2)
> indx1 <- indx[!is.na(indx)]
> indx2 <- match(vec2,vec1)
> indx2N <- indx2[!is.na(indx2)]
> m1[indx2N] <- m2[indx1]
>
> Thanks a lot!!
>
>
> Best,
>
>
> Elio
>

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] problem with previous code

2014-03-04 Thread arun


Hi Elio,

If you change the last line of the code:

m1[indx2N] <- m2[indx1]

to
m1[indx2N] <- m2[sort(indx1)]

 sum(m1[rownames(m1)=="p79",])
#[1] 191
 sum(m2[rownames(m2)=="p79",])
#[1] 191


The problem is in the order of the colnames/rownames in each of the datasets.  
I tested it on something like:
m1 <- matrix(0,12,12,dimnames=rep(list(paste0("a",1:12)),2))
 set.seed(49)
 m2 <- 
matrix(sample(0:2,4*4,replace=TRUE),ncol=4,dimnames=rep(list(c("a4","a6","a9","a12")),2))

which didn't show the problem.


But, if I change the order of colnames:

m1 <- matrix(0,12,12,dimnames=rep(list(paste0("a",c(1:3,8:12,4:7))),2))

set.seed(49)
 m2 <- 
matrix(sample(0:2,4*4,replace=TRUE),ncol=4,dimnames=rep(list(c("a9","a4","a6","a12")),2))
vec1 <- paste0(rownames(m1)[row(m1)],colnames(m1)[col(m1)])
vec2 <- paste0(rownames(m2)[row(m2)],colnames(m2)[col(m2)])
indx <- match(vec1,vec2)
indx1 <- indx[!is.na(indx)]

indx2 <- match(vec2,vec1)
indx2N <- indx2[!is.na(indx2)]
m1[indx2N] <- m2[sort(indx1)]
m1


Hope this helps.
A.K.






On Tuesday, March 4, 2014 3:16 PM, Elio Shijaku  wrote:

Hi Arun,

Sorry to disturb, but while trying your code to combine two matrices of unequal 
dimensions, I noticed a problem, the sum for each variable does not match, but 
the overall variables sum does match for both matrices, something isn't going 
right, perhaps the variables are getting mixed up, any idea??

Here is the code you sent me and the zipped files for testing:


dat1 <-
read.table("mtest.txt",header=TRUE)
dim(dat1)

dat2 <-
read.table("1998res_x.txt",header=TRUE)
dim(dat2)
m1 <- as.matrix(dat1)
m2 <- as.matrix(dat2)
vec1 <-
paste0(rownames(m1)[row(m1)],colnames(m1)[col(m1)])
vec2 <- paste0(rownames(m2)[row(m2)],colnames(m2)[col(m2)])
indx <- match(vec1,vec2)
indx1 <- indx[!is.na(indx)]
indx2 <- match(vec2,vec1)
indx2N <- indx2[!is.na(indx2)]
m1[indx2N] <- m2[indx1]

Thanks a lot!!


Best,


Elio

__
R-help@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.