[R] big edge list to adjacency matrix

Rui Barradas ruipbarradas at sapo.pt
Wed Mar 13 00:10:14 CET 2013


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



More information about the R-help mailing list