Hi,
You could try:
Either:

dat1 <- read.table("Test.txt",header=TRUE)
dim(dat1)
#[1] 4735 4735
dat2 <- read.table("1991res.txt",header=TRUE)
dim(dat2)
#[1] 574 574
m1 <- as.matrix(dat1)
m2 <- as.matrix(dat2)
library(data.table)
d1 <- 
data.table(Name1=as.vector(outer(rownames(m1),colnames(m1),paste0)),value1=as.vector(m1),key='Name1')
d2 <- 
data.table(Name1=as.vector(outer(rownames(m2),colnames(m2),paste0)),value2=as.vector(m2),key='Name1')
res <- d2[d1]
res1 <- as.data.frame(res)
res1[,3][!is.na(res1[,2])] <- res1[,2][!is.na(res1[,2])]
vec1 <- as.vector(outer(rownames(m1),colnames(m1),paste0))
res2 <- res1[match(vec1,res1[,1]),-2]
res3 <- matrix(res2[,2],dimnames=list(rownames(m1),colnames(m1)),ncol=ncol(m1))


#or

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]



A.K.


On Monday, December 16, 2013 1:54 PM, Elio Shijaku <sel...@gmail.com> wrote:

Hi Arun,

I hope you remember me. You helped me build several symmetrical matrices in R. 
I have one question though. I have now build all the matrices and I want to 
combine each of them to a larger one. To make the matter simpler to understand, 
I am attaching the text version of both files. What I would like is to merge 
the contents of 1991res (574x574 symmetric matrix) into the Test file 
(4703x4703, symmetric matrix which includes the row names of the 1991res. 

Could you help me by showing the steps I should take to merge the matrices?

Thank you in advance and sorry for disturbing.

Best,

Elio




On Mon, Sep 23, 2013 at 2:19 AM, arun <smartpink...@yahoo.com> wrote:

Hi Elio,
>
>There was only one matrix with that error.  Glad you were able to correct it.
>
>I sent an linkedin request to you.
>Regards
>Arun
>
>
>
>
>
>
>
>________________________________
>From: Elio Shijaku <sel...@gmail.com>
>To: arun <smartpink...@yahoo.com>
>Sent: Sunday, September 22, 2013 7:21 PM
>
>Subject: Re: binary symmetric matrix combination
>
>
>
>Hi Arun,
>
>You are very right, that mistake which i corrected by removal fixed the issue, 
>now the matrix works perfectly.
>
>Many thanks for all your help, please if you are in LinkedIn, I would be 
>delighted to add you as a friend.
>
>Here is my profile in case you're interested:
>es.linkedin.com/pub/elio-shijaku/11/b7b/147/
>
>Hopefully I can "disturb" you in case I have further questions, I am just 
>learning R and everything is new to me.
>
>All the best,
>
>Elio
>
>
>
>
>On Mon, Sep 23, 2013 at 12:05 AM, arun <smartpink...@yahoo.com> wrote:
>
>Hi,
>>Actually, you have duplicate names.
>>
>>
>>"p226 p226 s112" 
>>
>>What do you need to do in those situations?  Looks like it is a mistake in 
>>the file.
>>
>>
>>
>>
>>________________________________
>>From: Elio Shijaku <sel...@gmail.com>
>>To: arun <smartpink...@yahoo.com>
>>Sent: Sunday, September 22, 2013 5:41 PM
>>
>>Subject: Re: binary symmetric matrix combination
>>
>>
>>
>>Hi Arun,
>>
>>Here is the file, I tried many options, once I enter the command
>>
>>lst2<- lapply(lst1[lapply(lst1,length)>0],function(x) 
>>as.matrix(read.table(text=x,row.name=1)))
>>
>>
>>I get:
>>
>>Error in read.table(text = x, row.name = 1) :  duplicate 'row.names' are not 
>>allowed
>>
>>
>>Any idea? Thanks a lot for the help.
>>
>>
>>Elio
>>
>>
>>
>>
>>
>>On Sun, Sep 22, 2013 at 5:02 PM, arun <smartpink...@yahoo.com> wrote:
>>
>>Hi Elio,
>>>Check the new text file.  I used the first line:
>>>
>>>lines1<-str_trim(gsub("\t"," ",readLines("file.txt")))
>>>because the file was "\t" separated
>>>
>>>In the new file, it could be just space separated.  So, you may only need:
>>>lines1<- readLines("file.txt")
>>>
>>>If possible, could you email me the file.  I can take a look into it.
>>>A.K.
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>________________________________
>>>
>>>From: Elio Shijaku <sel...@gmail.com>
>>>To: arun <smartpink...@yahoo.com>
>>>Sent: Sunday, September 22, 2013 7:55 AM
>>>
>>>Subject: Re: binary symmetric matrix combination
>>>
>>>
>>>
>>>
>>>Hi Arun,
>>>
>>>I am replying to this e-mail address to not break the forum's rules. 
>>>Everything worked great. However, when I use another text file called 
>>>"file", I use the commands you gave me but I get this problem:
>>>
>>>library(stringr)
>>>
>>>lines1<-str_trim(gsub("\t"," ",readLines("file.txt")))
>>> lst1<-lapply(split(lines1,
>>>cumsum(lines1=="")),function(x) x[x!=""])
>>>
>>>
>>>lst2<- lapply(lst1[lapply(lst1,length)>0],function(x) 
>>>as.matrix(read.table(text=x,row.names=1)))
>>>
>>>Error in read.table(text = x, row.names = 1) :  duplicate 'row.names' are 
>>>not allowed
>>>
>>>#I cannot then continue with the rest:
>>>
>>>
>>>names(lst2)<- paste0("m",seq_along(lst2))
>>>dat<- do.call(rbind,lapply(names(lst2),function(x) {x1<- lst2[[x]]; 
>>>cbind(expand.grid(rep(list(colnames(x1)),2),stringsAsFactors=FALSE),value=as.vector(x1))}))
>>>library(reshape2)
>>>res<- dcast(dat,Var1~Var2,value.var="value",sum)
>>> row.names(res)<- res[,1]
>>> res<- as.matrix(res[,-1])
>>> dim(res)
>>>
>>>
>>>Any idea on why such problem?
>>>
>>>Thanks yet again
>>>
>>>
>>>
>>>
>>>On Wed, Sep 18, 2013 at 7:44 PM, <smartpink...@yahoo.com> wrote:
>>>
>>>Hi,
>>>>Forgot to comment about the second Error.
>>>>The error suggests 'Libro3.txt' is not found in your working directory.  
>>>>Change your working directory to where the file is present or copy and 
>>>>paste the file in the working directory.  If you want to change the WD, use:
>>>>?setwd().
>>>>
>>>>
>>>>
>>>><quote author='supernovartis'>
>>>>Hi Arun,
>>>>
>>>>Thanks for the promt reply.
>>>>
>>>>Yes the data is tab delimited. When I tried to use the command:
>>>>
>>>>> lst1<-lapply(split(lines1,cumsum(lines1=="")),function(x)
>>>>> as.matrix(read.table(text=x[x!=""],sep="",row.names=1)))
>>>>
>>>>I get:
>>>>
>>>>Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings,
>>>>:
>>>>  line 1 did not have 6 elements
>>>>
>>>>I tried the second command:  > lines2<- gsub("\t","",
>>>>readLines("Libro3.txt"))
>>>>
>>>>I get:
>>>>
>>>>Error in file(con, "r") : cannot open the connection
>>>>In addition: Warning message:
>>>>In file(con, "r") :
>>>>  cannot open file 'Libro3.txt': No such file or directory
>>>>
>>>>Obviously, the file is not in R directory, any option how to read it from
>>>>the desktop?
>>>>
>>>>Any help?
>>>>
>>>>Thanks again
>>>></quote>
>>>>Quoted from:
>>>>http://r.789695.n4.nabble.com/binary-symmetric-matrix-combination-tp4675440p4676437.html
>>>>
>>>>
>>>>_____________________________________
>>>>Sent from http://r.789695.n4.nabble.com
>>>>
>>>>
>>>
>>
>

______________________________________________
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.

Reply via email to