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

  • SEARCH
  • Home
  • 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 8243855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T21:39:35+00:00 2026-06-07T21:39:35+00:00

I would like to compare two lists (two rows of a data frame) and

  • 0

I would like to compare two lists (two rows of a data frame) and count how many differences I have between the two lists.

for instance:

list1=a,b,c,a
list2=a,a,d,d

two elements of list 1 are in list 2

I am able to do that with a loop and sum but it is very inefficient. Is there any function to do that in R?

I have checked setdiff and the compare package but did not find anything that helps.

Thanks for your ideas,

Vincent

My function looks like:

        NRebalancing=function(NamePresent)
        {
          Nbexchange=NamePresent[,2]
          Nbexchange=NamePresent[1,2]=0

          for (i in 2:nrow(NamePresent))
          {
            print(i)
            compteur=0
            NameNeeded=NamePresent[i,]
            NameNeeded=unique(NameNeeded)
            NameNeeded=na.omit(NameNeeded)
            for(j in 2:length(NameNeeded))
              #j=1 correspond a une date
            {
              compteur = compteur+(abs(sum(NamePresent[i,]==as.character(NameNeeded[j]))-sum(NamePresent[i-1,]==as.character(NameNeeded[j]))))
            }
          Nbexchange[i]=compteur  
          }

          return(Nbexchange)
        }
  • 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-07T21:39:36+00:00Added an answer on June 7, 2026 at 9:39 pm

    One main point: your list isn’t an R list – that’s something a bit special. You are using vectors:

    R> is.vector(l1)
    [1] TRUE
    R> is.list(l1)
    [1] FALSE
    

    don’t call variables list1 if they are vectors.


    Since you have a vector there are lots of possibilities open.

    1. The %in% operator

      R> l1 = c("a", "b", "c", "d")
      R> l2 = c("a", "a", "d", "d")
      R> l1[l1 %in% l2]
       [1] "a" "d"
      
    2. Or use is.element

      R> l1[is.element(l1, l2)]
       [1] "a" "d"
      
    3. There is also unique:

      R> unique(l2)
       [1] "a" "d"
      

      Following your comment to @mrdwab, you can count the number of occurances using a combination of sapply and unique

      sapply(unique(l1), function(i) sum(i==l2))
      

      i==l2 checks for membership, sum counts the number of times TRUE appears and sapply is basically just a for loop over unique(l1)

      R> sapply(unique(l1), function(i) sum(i==l2))
      a b c d 
      2 0 0 2
      
    4. A very nice suggestion from @mrdwab is to use table and colSums:

      R> table(l1, l2)
        l2 l1  
         a d
       a 1 0
       b 1 0
       c 0 1
       d 0 1
      R> colSums(table(l1, l2))
       a d 
       2 2 
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two SQL tables with data that I would like to compare. The
I have two images which i would like to compare and do different operations
I have two arrays that I would like to compare and ultimately wind up
I would like to compare two different sets of data on the same imshow
I would like to compare two columns in the same table. I want to
I would like to be able to compare two classes derived from the same
I would like to know if there is anyway I can compare two columns
I'm looking to compare two varchars in SQL, one would be something like Cafe
I'd like to align two lists in a similar way to what difflib.Differ would
I have run into a situation where I need to compare two different lists

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.