How to identify multiple identical pairs in two vectors
In my graph-package (as in graph theory, nodes connected by edges) I have a vector indicating for each edge the node of origin from
, a vector indicating for each edge the node of destination to
and a vector indicating the curve of each edge curve
.
By default I want edges to have a curve of 0 if there is only one edge between two nodes and curve of 0.2 if there are two edges between two nodes. The code that I use now is a for-loop, and it is kinda slow:
curve <- rep(0,5)
from<-c(1,2,3,3,2)
to<-c(2,3,4,2,1)
for (i in 1:length(from))
{
if (any(from==to[i] & to==from[i]))
{
curve[i]=0.2
}
}
So basically I look for each edge (one index in from
and one in to
) if there is any other pair in from
and to
that use the same nodes (numbers).
What I am looking for are two things:
- A way to identify if there is any pair of nodes that have two edges between them (so I can omit the loop if not)
- A way to speed up this loop
#
EDIT:
To make this abit clearer, another example:
from <- c(4L, 6L, 7L, 8L, 1L, 9L, 5L, 1L, 2L, 1L, 10L, 2L, 6L, 7L, 10L, 4L, 9L)
to <- c(1L, 1L, 1L, 2L, 3L, 3L, 4L, 5L, 6L, 7L, 7L, 8L, 8L, 8L, 8L, 10L, 10L)
cbind(from,to)
from to
[1,] 4 1
[2,] 6 1
[3,] 7 1
[4,] 8 2
[5,] 1 3
[6,] 9 3
[7,] 5 4
[8,] 1 5
[9,] 2 6
[10,] 1 7
[11,] 10 7
[12,] 2 8
[13,] 6 8
[14,] 7 8
[15,] 10 8
[16,] 4 10
[17,] 9 10
In these two vectors, pair 3 is identical to pair 10 (both 1 and 7 in different orders) and pairs 4 and 12 are identical (both 2 and 8). So I would want curve
to become:
[1,] 0.0
[2,] 0.0
[3,] 0.2
[4,] 0.2
[5,] 0.0
[6,] 0.0
[7,] 0.0
[8,] 0.0
[9,] 0.0
[10,] 0.2
[11,] 0.0
[12,] 0.2
[13,] 0.0
[14,] 0.0
[15,] 0.0
[16,] 0.0
[17,] 0.0
(as vector, I transposed twice to get row numbers).
Solution
from <- c(4L, 6L, 7L, 8L, 1L, 9L, 5L, 1L, 2L, 1L, 10L, 2L, 6L, 7L, 10L, 4L, 9L)
to <- c(1L, 1L, 1L, 2L, 3L, 3L, 4L, 5L, 6L, 7L, 7L, 8L, 8L, 8L, 8L, 10L, 10L)
srt <- apply(cbind(from,to),1,sort)
dub <- duplicated(t(srt))|duplicated(t(srt),fromLast=T)
curve <- ifelse(dub,0.2,0)
Benchmarking solutions
Here is some benchmarking of different solutions
> # for-loop
> system.time(
+ {
+ curve <- rep(0,5)
+ for (i in 1:length(from))
+ {
+ if (any(from==to[i] & to==from[i]))
+ {
+ curve[i]=0.2
+
+ }
+ }
+ })
user system elapsed
171.49 0.05 171.98
from <- sample(1:1000,100000,T)
> to <- sample(1:1000,100000,T)
>
> # My solution:
> system.time(
+ {
+ srt <- apply(cbind(from,to),1,sort)
+ dub <- duplicated(t(srt))|duplicated(t(srt),fromLast=T)
+ curve <- ifelse(dub,0.2,0)
+ })
user system elapsed
16.92 0.00 16.94
>
>
> # Marek 1:
> system.time(
+ {
+ srt <- cbind(pmin(from,to), pmax(from,to) )
+ dub <- duplicated(srt)|duplicated(srt,fromLast=T)
+ curve <- ifelse(dub,0.2,0)
+ })
user system elapsed
2.43 0.00 2.43
>
> # Marek 2:
> system.time(
+ {
+ srt <- cbind(ifelse(from>to,to,from),ifelse(from>to,from,to))
+ dub <- duplicated(srt)|duplicated(srt,fromLast=T)
+ curve <- ifelse(dub,0.2,0)
+ })
user system elapsed
2.67 0.00 2.70
>
> # Maiasaura:
> library(plyr)
>
> system.time(
+ {
+ data=data.frame(cbind(id=1:length(from),from,to))
+ data=ddply(data, .(id), transform, f1=min(from,to),f2=max(from,to))
+ curved=data.frame(data[which(duplicated(data[,4:5])==TRUE),],value=0.2)
+ result=join(data[,4:5],curved[,4:6],by=intersect(names(data)[4:5],names(curved)[4:6]))
+ result$value[which(is.na(result$value))]=0
+ result=data.frame(from,to,curve=result$value)
+ })
user system elapsed
103.43 0.11 103.95
> # Marek 1 + Joshua
> > system.time(
> + {
> + srt <- cbind(pmin(from,to), pmax(from,to) )
> + curve <- ifelse(ave(srt[,1], srt[,1], srt[,2], FUN=length) > 1,
> 0.2, 0)
> + }) user system elapsed
> 7.26 0.00 开发者_开发百科7.25
which gives the fastest solution being:
srt <- cbind(pmin(from,to), pmax(from,to) )
dub <- duplicated(srt)|duplicated(srt,fromLast=T)
curve <- ifelse(dub,0.2,0)
Here is a solution using plyr
I first combine from
and to
into a data.frame
library(plyr)
data=data.frame(cbind(id=1:length(from),from,to))
data
id from to
1 1 4 1
2 2 6 1
3 3 7 1
4 4 8 2
5 5 1 3
6 6 9 3
7 7 5 4
8 8 1 5
9 9 2 6
10 10 1 7
11 11 10 7
12 12 2 8
13 13 6 8
14 14 7 8
15 15 10 8
16 16 4 10
17 17 9 10
then the following should produce the result you seek:
data=ddply(data, .(id), transform, f1=min(from,to),f2=max(from,to))
curved=data.frame(data[which(duplicated(data[,4:5])==TRUE),],value=0.2)
result=join(data[,4:5],curved[,4:6],by=intersect(names(data)[4:5],names(curved)[4:6]))
result$value[which(is.na(result$value))]=0
result=data.frame(from,to,curve=result$value)
should produce:
from to curve
1 4 1 0.0
2 6 1 0.0
3 7 1 0.2
4 8 2 0.2
5 1 3 0.0
6 9 3 0.0
7 5 4 0.0
8 1 5 0.0
9 2 6 0.0
10 1 7 0.2
11 10 7 0.0
12 2 8 0.2
13 6 8 0.0
14 7 8 0.0
15 10 8 0.0
16 4 10 0.0
17 9 10 0.0
You can turn the above code into a function
calculate_curve <- function (from,to)
{
data=data.frame(cbind(id=1:length(from),from,to))
data=ddply(data, .(id), transform, f1=min(from,to),f2=max(from,to))
curved=data.frame(data[which(duplicated(data[,4:5])==TRUE),],value=0.2)
result=join(data[,4:5],curved[,4:6],by=intersect(names(data)[4:5],names(curved)[4:6]))
result$value[which(is.na(result$value))]=0
return (result$value)
}
and just do
curve=calculate_curve(from,to)
How about using outer
?
from <- c(1,2,3,3,2)
to <- c(2,3,4,2,1)
out <- outer(from, to, `==`)
ifelse(rowSums(out) > 0 & colSums(out) > 0, 0.2, 0)
Changing
any(from==to[i] & to==from[i])
to
any(from==to[i]) && any(to==from[i])
can save quite a bit of time. In your example, if from
and to
are replicated 5000 times, computation time is reduced by 1/3.
When using &&
, if the first condition is FALSE
R doesn't bother to evaluate the second expression.
If I understand correctly, you could use %in%
:
curve[ to %in% from & from %in% to ] <- 0.2
Another solution based on your update:
srt <- t(apply(cbind(from,to),1,sort))
curve <- ifelse(ave(srt[,1], srt[,1], srt[,2], FUN=length) > 1, 0.2, 0)
精彩评论