rdorlearn -4 years ago 80
R Question

# match and count values in sequence by group in R

Here is my data:

group <- c(1,1,1,1,2,2,2,3,3,4,4,4,4)
X1 <- c("A","A","A","A","B","A","B","A","A","B","B","B","B")
X2 <- c("A","A","A","A","B","B","B","A","A","B","B","A","A")
X3 <- c("B","A","A","A","B","B","B","B","B","B","B","B","B")
X4 <- c("A","A","A","B","B","B","A","A","A","B","A","B","B")
X5 <- c("A","A","A","A","B","B","B","A","A","A","B","B","B")
X6 <- c("A","A","A","A","B","A","B","A","A","B","B","A","A")
mydf <- data.frame (group, X1, X2, X3, X4, X5, X6)

Thus data is:

group X1 X2 X3 X4 X5 X6
1 1 A A B A A A
2 1 A A A A A A
3 1 A A A A A A
4 1 A A A B A A
5 2 B B B B B B
6 2 A B B B B A
7 2 B B B A B B
8 3 A A B A A A
9 3 A A B A A A
10 4 B B B B A B
11 4 B B B A B B
12 4 B A B B B A
13 4 B A B B B A

Now I need to compare the first row with rest of rows in group.

group X1 X2 X3 X4 X5 X6
1 1 A A B A A A
2 1 A A A A A A
TRUE TRUE FALSE TRUE TRUE TRUE

Here mismatch is only at X3. 1 out of 6 = 1/6 = 17%

Similarly compare 3 with 1st in group 1.

group X1 X2 X3 X4 X5 X6
1 1 A A B A A A
3 1 A A A A A A

mismatch = 17%

Also compare 4 with 1st in group 1.

group X1 X2 X3 X4 X5 X6
1 1 A A B A A A
4 1 A A A B A A

mismatch = 2/6 = 34%

Similarly for group 2 (1st row of group to i.e. 5 with 6)

group X1 X2 X3 X4 X5 X6
5 2 B B B B B B
6 2 A B B B B A

mismatch = 2/6 = 34%

Similarly:

group X1 X2 X3 X4 X5 X6
5 2 B B B B B B
7 2 B B B A B B

mismatch = 1/6 = 17%

My trial:

match (mydf[1,], mydf[2,])
match (mydf[1,], mydf[3,])

Try this:

match_ratio <- function(x)
cbind(x, match_ratio = rowMeans(mapply(`==`, x[1, -1], x[, -1])))
library(plyr)
ddply(mydf, "group", match_ratio)

#    group X1 X2 X3 X4 X5 X6 match_ratio
# 1      1  A  A  B  A  A  A   1.0000000
# 2      1  A  A  A  A  A  A   0.8333333
# 3      1  A  A  A  A  A  A   0.8333333
# 4      1  A  A  A  B  A  A   0.6666667
# 5      2  B  B  B  B  B  B   1.0000000
# 6      2  A  B  B  B  B  A   0.6666667
# 7      2  B  B  B  A  B  B   0.8333333
# 8      3  A  A  B  A  A  A   1.0000000
# 9      3  A  A  B  A  A  A   1.0000000
# 10     4  B  B  B  B  A  B   1.0000000
# 11     4  B  B  B  A  B  B   0.6666667
# 12     4  B  A  B  B  B  A   0.5000000
# 13     4  B  A  B  B  B  A   0.5000000
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download