Hi,
You could also do:
library(reshape2)
?dcast(dat,V1~V2,value.var="V3",max,fill=0)
#? V1 b1 b2 b3
#1 a1? 4? 0? 0
#2 a2? 0? 2? 0
#3 a3? 5? 0? 3
A.K.
----- Original Message -----
From: Rui Barradas <ruipbarradas at sapo.pt>
To: avinash sahu <avinash.sahu at gmail.com>
Cc: r-help at r-project.org
Sent: Tuesday, March 12, 2013 7:10 PM
Subject: Re: [R] big edge list to adjacency matrix
Hello,
The following is a bit convoluted but will do it.
dat <- read.table(text = "
a1 b1 1
a2 b2 2
a3 b3 3
a1 b1 4
a3 b1 5
")
xtabs(V3 ~ V1 + V2, data = aggregate(V3 ~ V1 + V2, data = dat, FUN = max))
Hope this helps,
Rui Barradas
Em 12-03-2013 21:45, avinash sahu escreveu:> I have huge list of edges with weights.
> a1 b1 w1
> a2 b2 w2
> a3 b3 w3
> a1 b1 w4
> a3 b1 w5
> I have to convert it into 2 dim matrix
>? ? ? b1? ? ? ? ? ? ? ? b2? ? ? ? ? b3
> a1? max(w1,w4)? 0? ? ? ? ? 0
> a2? ? ? ? ? ? 0? ? ? ? w2? ? ? ? 0
> a3? ? ? ? w5? ? ? ? ? 0? ? ? ? ? w3
>
> if edges repeated take the maximum weights. How do this efficiently without
> using for loop? Any idea.
>
> thanks
> Avi
>
> ??? [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>
______________________________________________
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.