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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T14:52:32+00:00 2026-05-21T14:52:32+00:00

I have a nested list in say lst (all the elements are of class

  • 0

I have a nested list in say lst(all the elements are of class int). I don’t know the length of lst in advance; however I do know that each element of lst is a list of length say k

length(lst[[i]]) # this equals k and is known in advance, 
                 # this is true for i = 1 ... length(lst)

How do I take the union of the 1st element, 2nd element, …, kth element of all the elements of lst

Specifically, if the length of lst is n, I want (not R code):

# I know that union can only be taken for 2 elements, 
# following is for illustration purposes
listUnion1 <- union(lst[[1, 1]], lst[[2, 1]], ..., lst[[n, 1]])
listUnion2 <- union(lst[[1, 2]], lst[[2, 2]], ..., lst[[n, 2]])
.
.
.
listUnionk <- union(lst[[1, k]], lst[[2, k]], ..., lst[[n, k]])

Any help or pointers are greatly appreciated.

Here is a dataset that can be used, n = 3 and k = 2

list(structure(list(a = 1:5, b = 6:11), .Names = c("a", "b")), 
    structure(list(a = 6:11, b = 1:5), .Names = c("a", "b")), 
    structure(list(a = 12, b = 12), .Names = c("a", "b")))
  • 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-05-21T14:52:32+00:00Added an answer on May 21, 2026 at 2:52 pm

    Here is a general solution, similar in spirit to that of @Ramnath, but avoiding the use of union() which is a binary function. The trick is to note that union() is implemented as:

    unique(c(as.vector(x), as.vector(y)))
    

    and the bit inside unique() can be achieved by unlisting the nth component of each list.

    The full solution then is:

    unionFun <- function(n, obj) {
        unique(unlist(lapply(obj, `[[`, n)))
    }
    lapply(seq_along(lst[[1]]), FUN = unionFun, obj = lst)
    

    which gives:

    [[1]]
     [1]  1  2  3  4  5  6  7  8  9 10 11 12
    
    [[2]]
     [1]  6  7  8  9 10 11  1  2  3  4  5 12
    

    on the data you showed.

    A couple of useful features of this are:

    • we use `[[` to subset obj in unionFun. This is similar to function(x) x$a in @Ramnath’s Answer. However, we don’t need an anonymous function (we use `[[` instead). The equivalent to @Ramnath’s Answer is: lapply(lst, `[[`, 1)
    • to generalise the above, we replace the 1 above with n in unionFun(), and allow our list to be passed in as argument obj.

    Now that we have a function that will provide the union of the nth elements of a given list, we can lapply() over the indices k, applying our unionFun() to each sub-element of lst, using the fact that the length of lst[[1]] is the same as length(lst[[k]]) for all k.

    If it helps to have the names of the nth elements in the returned object, we can do:

    > unions <- lapply(seq_along(lst[[1]]), FUN = unionFun, obj = lst)
    > names(unions) <- names(lst[[1]])
    > unions
    $a
     [1]  1  2  3  4  5  6  7  8  9 10 11 12
    
    $b
     [1]  6  7  8  9 10 11  1  2  3  4  5 12
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a nested unordered list that I would like to serialize
I have a nested list like this: <ul class=list> <li class=list_item_type_1> <ul class=list> <li
I have Python nested list that I'm trying to organize and eventually count number
How can I count nested list elements in Prolog? I have the following predicates
Lets say I have an unordered nested list: <ul> <li>Item a1</li> <li>Item a2</li> <li>Item
I have a nested list that I am using in Robot Framework. I would
Let's say I have a short list of strings, that can contain duplicates: <A,
I have a nested list of data. Its length is 132 and each item
I have a 2-dimensional list of named tuples (let's say that each tuple has
Lets say I have this list: lst = [[0], [0, 0], [0, 0, 0],

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.