Skip to content

matrix of all difference between rows values

6 messages · Rui Barradas, cleberchaves, arun

#
Hello all,
i would like to calculate the difference of all row values and the others
row values from my matrix (table 1). The output (table 2) would be a matrix
with input matrix's row names on row names and colums names, thereby the
difference values among two of the row names could be bether found.

Could someone help me?

Examples:

         Table 1:
         a         10
         b         9
         c         8
         d         7
         e         6


         Table 2:
                  a         b         c         d         e
         a       0        -1        -2        -3        -4
         b       1        0         -1        -2        -3
         c        2        1         0        -1         -2
         d       3        2         1          0         1
         e       4        3         2          1          0



--
View this message in context: http://r.789695.n4.nabble.com/matrix-of-all-difference-between-rows-values-tp4649191.html
Sent from the R help mailing list archive at Nabble.com.
#
Hello,

Try the following.

# Create the dataset
Table1 <- matrix(10:6, ncol = 1)
rownames(Table1) <- letters[1:5]
Table1

t(outer(Table1[,1], Table1[,1], `-`))

Hope this helps,

Rui Barradas
Em 10-11-2012 18:32, cleberchaves escreveu:
#
Hi,

Though the results from Rui, and me are similar, may be it differs in other instances.

My result:?
dat1<-read.table(text="
?a???????? 10
???????? b???????? 9
???????? c???????? 8
???????? d???????? 7
???????? e???????? 6
",sep="",header=FALSE,stringsAsFactors=FALSE)
?res1<-apply(toeplitz(dat1[,2]),1,function(x) 10-x)
res1[upper.tri(res1)]<- -1*res1[upper.tri(res1)]
row.names(res1)<-dat1[,1]
?colnames(res1)<-dat1[,1]
?res1 
# ?a ?b ?c ?d ?e 
#a 0 -1 -2 -3 -4 
#b 1 ?0 -1 -2 -3 
#c 2 ?1 ?0 -1 -2 
#d 3 ?2 ?1 ?0 -1 
#e 4 ?3 ?2 ?1 ?0 

Rui's solution: 
?t(outer(Table1[,1],Table1[,1],"-")) 
# ?a ?b ?c ?d ?e 
#a 0 -1 -2 -3 -4 
#b 1 ?0 -1 -2 -3 
#c 2 ?1 ?0 -1 -2 
#d 3 ?2 ?1 ?0 -1 
#e 4 ?3 ?2 ?1 ?0 


I guess ?expand.grid() might also help you.
dat2<-expand.grid(dat1[,2],dat1[,2])
mat1<-matrix(apply(dat2,1,diff),ncol=5)
dimnames(mat1)<-dimnames(res1)
?mat1
#? a? b? c? d? e
#a 0 -1 -2 -3 -4
#b 1? 0 -1 -2 -3
#c 2? 1? 0 -1 -2
#d 3? 2? 1? 0 -1
#e 4? 3? 2? 1? 0


A.K.




----- Original Message -----
From: cleberchaves <cleberchaves at gmail.com>
To: r-help at r-project.org
Cc: 
Sent: Saturday, November 10, 2012 3:55 PM
Subject: Re: [R] matrix of all difference between rows values

Mmmm...

Actually, Rui Barradas is the right!

Arun kirshna, yours script has an error. That repeat the same set of numbers
in all columns...

Anyway, thanks for both!



--
View this message in context: http://r.789695.n4.nabble.com/matrix-of-all-difference-between-rows-values-tp4649191p4649207.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
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.
#
Hi Arun,
i don't know exactly the error of yours script. 
Maybe when i changed from "10-x" to "dat1[1,2]-x" (because my real matrix
does not start with 10) the error has appeared, the same numbers repeat in
all columns.
Maybe when i change for your second script that error does not appear again.
Nevertheless, thanks again, Arun!

Cleber Chaves



--
View this message in context: http://r.789695.n4.nabble.com/matrix-of-all-difference-between-rows-values-tp4649191p4649241.html
Sent from the R help mailing list archive at Nabble.com.
#
Hi, 

?I tried with another example and was able to understand the error.? Sorry, I didn't test the code to other situations.
For example:

vec1<-c(8,7,10,15,1,3,6)
#According to my first solution:
res1<-apply(toeplitz(vec1),1,function(x) vec1[1]-x)
?res1
#???? [,1] [,2] [,3] [,4] [,5] [,6] [,7]
#[1,]??? 0??? 1?? -2?? -7??? 7??? 5??? 2
#[2,]??? 1??? 0??? 1?? -2?? -7??? 7??? 5
#[3,]?? -2??? 1??? 0??? 1?? -2?? -7??? 7
#[4,]?? -7?? -2??? 1??? 0??? 1?? -2?? -7
#[5,]??? 7?? -7?? -2??? 1??? 0??? 1?? -2
#[6,]??? 5??? 7?? -7?? -2??? 1??? 0??? 1
#[7,]??? 2??? 5??? 7?? -7?? -2??? 1??? 0

#Rui's code:
?Table1<-matrix(vec1,ncol=1)
?t(outer(Table1[,1],Table1[,1],"-"))
#or simply
?t(outer(vec1,vec1,"-"))
#???? [,1] [,2] [,3] [,4] [,5] [,6] [,7]
#[1,]??? 0?? -1??? 2??? 7?? -7?? -5?? -2
#[2,]??? 1??? 0??? 3??? 8?? -6?? -4?? -1
#[3,]?? -2?? -3??? 0??? 5?? -9?? -7?? -4
#[4,]?? -7?? -8?? -5??? 0? -14? -12?? -9
#[5,]??? 7??? 6??? 9?? 14??? 0??? 2??? 5
#[6,]??? 5??? 4??? 7?? 12?? -2??? 0??? 3
#[7,]??? 2??? 1??? 4??? 9?? -5?? -3??? 0


#using expand.grid()
matrix(apply(expand.grid(vec1,vec1),1,diff),ncol=7)
?# ?? [,1] [,2] [,3] [,4] [,5] [,6] [,7]
#[1,]??? 0?? -1??? 2??? 7?? -7?? -5?? -2
#[2,]??? 1??? 0??? 3??? 8?? -6?? -4?? -1
#[3,]?? -2?? -3??? 0??? 5?? -9?? -7?? -4
#[4,]?? -7?? -8?? -5??? 0? -14? -12?? -9
#[5,]??? 7??? 6??? 9?? 14??? 0??? 2??? 5
#[6,]??? 5??? 4??? 7?? 12?? -2??? 0??? 3
#[7,]??? 2??? 1??? 4??? 9?? -5?? -3??? 0


?identical(t(outer(vec1,vec1,"-")),matrix(apply(expand.grid(vec1,vec1),1,diff),ncol=7))
#[1] TRUE
A.K.





----- Original Message -----
From: cleberchaves <cleberchaves at gmail.com>
To: r-help at r-project.org
Cc: 
Sent: Sunday, November 11, 2012 7:07 AM
Subject: Re: [R] matrix of all difference between rows values

Hi Arun,
i don't know exactly the error of yours script. 
Maybe when i changed from "10-x" to "dat1[1,2]-x" (because my real matrix
does not start with 10) the error has appeared, the same numbers repeat in
all columns.
Maybe when i change for your second script that error does not appear again.
Nevertheless, thanks again, Arun!

Cleber Chaves



--
View this message in context: http://r.789695.n4.nabble.com/matrix-of-all-difference-between-rows-values-tp4649191p4649241.html
Sent from the R help mailing list archive at Nabble.com.

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