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 8079551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:08:28+00:00 2026-06-05T16:08:28+00:00

I’ve got two long lists A and B which have the same length but

  • 0

I’ve got two long lists A and B which have the same length but contain different numbers of equivalent elements:
List A can contain many elements which also can recur in the same field.
List B either contains only one element or an empty field, i.e. “character(0)”.
A also contains some empty fields but for these records there’s always an element present in B, so there are no records with empty fields in A and B.
I want to combine the elements of A and B into a new list of the same length, C, according to the following rules:

  • All elements from A have to be present in C – including their potential recurrences in the same field.
  • If B contains an element which isn’t already present in A of the same record it’ll be added to C as well.
  • But if B contains an element which already is present in A of the same record it’ll be ignored.
  • If A has an empty field the element from B for this record will be added to C.
  • If B has an empty field the element(s) from A for this record will be added to C.

This is an example of how these lists begin:

> A  
 [1] "JAMES" "JAMES"  
 [2] "JOHN" "ROBERT"  
 [3] "WILLIAM" "MICHAEL" "WILLIAM" "DAVID" "WILLIAM"  
 [4] character(0)  
...  
> B  
 [1] "RICHARD"  
 [2] "JOHN"  
 [3] character(0)  
 [4] "CHARLES"  
...  

This is the correct output I’m looking for:

> C  
 [1] "JAMES" "JAMES" "RICHARD"  
 [2] "JOHN" "ROBERT"  
 [3] "WILLIAM" "MICHAEL" "WILLIAM" "DAVID" "WILLIAM"  
 [4] "CHARLES"  
... 

I tried, e.g.:

C <- sapply(mapply(union, A,B), setdiff, character(0))  

But this deleted the recurrences from A, unfortunately:

> C  
 [1] "JAMES" "RICHARD"  
 [2] "JOHN" "ROBERT"  
 [3] "WILLIAM" "MICHAEL" "DAVID"  
 [4] "CHARLES"  
...  

Can anybody tell me, please, how to combine these two lists, preserve the recurrences from A, and achieve the output I desire?

Thank you very much in advance!

Update: Machine readable data:

A <- list(c("JAMES","JAMES"),
          c("JOHN","ROBERT"), 
          c("WILLIAM","MICHAEL","WILLIAM","DAVID","WILLIAM"),  
          character(0))
B <- list("RICHARD","JOHN",character(0),"CHARLES")
  • 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-05T16:08:33+00:00Added an answer on June 5, 2026 at 4:08 pm

    Here is your snippte of data, in reproducible form:

    A <- list(c("JAMES","JAMES"),
              c("JOHN","ROBERT"), 
              c("WILLIAM","MICHAEL","WILLIAM","DAVID","WILLIAM"),  
              character(0))
    B <- list("RICHARD","JOHN",character(0),"CHARLES")
    

    You were close with mapply(). I got the desired output by using c() to concatenate the list elements in A and B but had to manipulate elements of the supplied vectors, so I came up with this:

    foo <- function(...) {
        l1 <- length(..1)
        l2 <- length(..2)
        out <- character(0)
        if(l1 > 0) {
            if(l2 > 0) {
                out <- if(..2 %in% ..1)
                    ..1
                else
                    c(..1, ..2)
            } else {
                out <-  ..1
            }
        } else {
            out <-  ..2
        }
        out
    }
    

    We can refer to the individual elements of ... using the ..n placeholders; ..1 is A and ..2 is B. Of course, foo() only works with two lists but doesn’t enforce this or do any checking, just to keep things simple. foo() also needs to handle the cases where either A or B or both are character(0) which I now think foo() does.

    When we use that in the mapply() call I get:

    > mapply(foo, A, B)
    [[1]]
    [1] "JAMES"   "JAMES"   "RICHARD"
    
    [[2]]
    [1] "JOHN"   "ROBERT"
    
    [[3]]
    [1] "WILLIAM" "MICHAEL" "WILLIAM" "DAVID"   "WILLIAM"
    
    [[4]]
    [1] "CHARLES"
    

    An lapply() version may be more meaningful than the abstract ..n but uses essentially the same code. Here is a new function that works with A and B directly but we iterate over the indices of the elements of A (1, 2, 3, length(A)) as generated by seq_along():

    foo2 <- function(ind, A, B) {
        l1 <- length(A[[ind]])
        l2 <- length(B[[ind]])
        out <- character(0)
        if(l1 > 0) {
            if(l2 > 0) {
                out <- if(B[[ind]] %in% A[[ind]]) {
                    A[[ind]]
                } else {
                    c(A[[ind]], B[[ind]])
                }
            } else {
                out <- A[[ind]]
            }
        } else {
            out <- B[[ind]]
        }
        out
    }
    

    which is called like this:

    > lapply(seq_along(A), foo2, A = A, B = B)
    [[1]]
    [1] "JAMES"   "JAMES"   "RICHARD"
    
    [[2]]
    [1] "JOHN"   "ROBERT"
    
    [[3]]
    [1] "WILLIAM" "MICHAEL" "WILLIAM" "DAVID"   "WILLIAM"
    
    [[4]]
    [1] "CHARLES"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I have a text area in my form which accepts all possible characters from
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small

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.