简体   繁体   中英

Comparing rows/vectors for identity in R using BY or APPLY?

I am trying to compare a series of rows and a fixed vector in R and flag them as identical (TRUE) or not identical (FALSE). Imagine the problem as comparing a set of test responses to an answer key.

I have been able to get the comparison to work using a loop and the COMPARE package, but I have been unable to duplicate it using more efficient methods such as BY, APPLY or DDPLY:

test.answers <- as.data.frame(rbind(c(ID="A",rep(3, 6)), c(ID="B",6:1), c(ID="C",1:6)))

library(compare)

# Compare using a loop
for (i in 1:length(test.answers)) 
  {
  test.answers$Static1[i] <- isTRUE(compare(as.data.frame(rbind(rep(3, 6))),
                                            test.answers[i,2:7], allowAll=TRUE))
  }
test.answers # This is correct!

staticfn <- function(x) 
  { 
  isTRUE(compare(as.data.frame(rbind(rep(3, 6))),
                 test.answers[2:7], allowAll=TRUE)) 
  }

# Compare using APPLY
test.answers$Static2 <- apply(test.answers, 1, staticfn)
test.answers # This is incorrect!

# Compare using BY
test.answers$Static3 <- by(test.answers, test.answers$ID, staticfn)
test.answers # This is incorrect!

# Compare using DDPLY
library(plyr)
test.answers <- ddply(test.answers, .(ID), { staticfn })
test.answers # This is incorrect!


# Results

  ID V2 V3 V4 V5 V6 V7 Static1 Static2 Static3
1  A  3  3  3  3  3  3    TRUE    TRUE    TRUE
2  B  6  5  4  3  2  1   FALSE    TRUE    TRUE
3  C  1  2  3  4  5  6   FALSE    TRUE    TRUE

  ID   V1
1  A TRUE
2  B TRUE
3  C TRUE

I'd be grateful if someone could suggest why APPLY and DDPLY are giving me a different result than the loop, and how I could modify one of these functions to avoid using a loop.

You're making this much too complicated:

apply(test.answers,1,function(x){all(x[-1] == rep(3,6))})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM