Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7759425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:37:32+00:00 2026-06-01T13:37:32+00:00

I am trying to compare a series of rows and a fixed vector in

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T13:37:34+00:00Added an answer on June 1, 2026 at 1:37 pm

    You’re making this much too complicated:

    apply(test.answers,1,function(x){all(x[-1] == rep(3,6))})
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to compare and print only members with Inactive status. The problem is that
I am trying to compare values on data frame rows, and removing all the
Im trying to 'compare' all documents between 2 collections, which will return true only
I am trying to parse a series of text files and save them as
I am trying to compare a series of strings like the following rodeo rodas
I´m trying to compare the rows, with the current date and hour, $curDate =
I'm trying to compare the dates and i'm having some problem with <= operator
I'm trying to compare hashes using Python, but I'm stuck with this problem: print
When trying to compare software versions 5.12 to 5.8, version 5.12 is newer, however
Im trying to compare these two chars but on win 32 Visual Studio 2008:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.