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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:50:24+00:00 2026-06-13T04:50:24+00:00

is it possible to directly select a column of all nested lists within a

  • 0

is it possible to directly select a column of all nested lists within a list?
My list is created using aggregate() with table():

AgN=aggregate(data,by=list(d$date),FUN=table,useNA="no")

AgN$x looks like:

$`0`

      1           2           3           9          11 
0.447204969 0.438509317 0.096894410 0.009937888 0.007453416 

$`1`

          1           2           4           8          11 
0.489974937 0.389724311 0.102756892 0.006265664 0.011278195 

…

$n

I want to get a vector of a specific column of each table, e.g. a vector containing the values of all columns named “1”.
I am still a R beginner, but even after searching and trying for a long time I found no nice solution. If I want to get the field of a list, I can simply index it with brackets, e.g. [i,j].
Online I found some examples for matrices, so I tried to do the same, at first only selecting one nested list’s column with AgN$x[1][1], but that is still selecting a whole list:

$0

     1           2           3           8          11 

0.447204969 0.438509317 0.096894410 0.009937888 0.007453416

My next try was AgN$x[[1]][1], and it was working:

  1 

0.447205

So I tried to to the same to select the value of each first column of all nested lists:

AgN$x[[1:length(AgN$x]][1]
Recursive indexing failed at level 2

Appearently the problem is that it is forbidden to select a range if you use a double brackets.

My last try was to use an for loop:

cduR=NULL 
for (i in 1:length(AgN$x)){
t=AgN$x[[i]]
cduR=c(cduR,as.vector(t["1"]))
}

Finally, so far that seems to working. But that way I had to build a loop each time when I want to select columns. Is there no direct way?

Thanks for your help.

  • 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-13T04:50:25+00:00Added an answer on June 13, 2026 at 4:50 am

    Assuming you have something like the following:

    myList <- list(`0` = c(`1` = 10, `2` = 20, `3` = 30, `4` = 72),
                   `1` = c(`1` = 15, `2` = 9, `3` = 7))
    myList
    # $`0`
    #  1  2  3  4 
    # 10 20 30 72 
    # 
    # $`1`
    #  1  2  3 
    # 15  9  7 
    

    Use sapply() or lapply() to get into your list and extract whatever columns you want. Some examples.

    # As a list of one-column data.frames
    lapply(myList, `[`, 1)
    # $`0`
    #  1 
    # 10 
    # 
    # $`1`
    #  1 
    # 15 
    
    # As a list of vectors
    lapply(myList, `[[`, 1)
    # $`0`
    # [1] 10
    # 
    # $`1`
    # [1] 15
    
    # As a named vector
    sapply(myList, `[[`, 1)
    #  0  1 
    # 10 15 
    
    # As an unnamed vector
    unname(sapply(myList, `[[`, 1))
    # [1] 10 15
    

    Other variants of the syntax that also get you there include:

    ## Same output as above, different syntax
    lapply(myList, function(x) x[1])
    lapply(myList, function(x) x[[1]])
    sapply(myList, function(x) x[[1]])
    unname(sapply(myList, function(x) x[[1]]))
    

    A Nested List Example

    If you do have nested lists (lists within lists), try the following variants.

    # An example nested list
    myNestedList <- list(A = list(`0` = c(`1` = 10, `2` = 20, `3` = 30, `4` = 72),
                                  `1` = c(`1` = 15, `2` = 9, `3` = 7)),
                         B = list(`0` = c(A = 11, B = 12, C = 13),
                                  `1` = c(X = 14, Y = 15, Z = 16)))
    
    # Run the following and see what you come up with....
    lapply(unlist(myNestedList, recursive = FALSE), `[`, 1)
    lapply(unlist(myNestedList, recursive = FALSE), `[[`, 1)
    sapply(unlist(myNestedList, recursive = FALSE), `[[`, 1)
    rapply(myNestedList, f=`[[`, ...=1, how="unlist")
    

    Note that for lapply() and sapply() you need to use unlist(..., recursive = FALSE) while for rapply() (recursive apply), you refer to the list directly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How to select all records from table apart from the last 100
I was wondering if its possible to directly cast objects within a foreach loop.
It is possible to call a table-valued function directly, or do I need to
I would like to select every column of a table, but want to have
Is is possible to select specific text string on the page directly without a
Possible Duplicate: Referring to a Column Alias in a WHERE Clause SELECT Trade.TradeId, Isnull(Securities.SecurityType,'Other')
I have a text[] ARRAY column within a PostgreSQL table and I need to
Is it possible to do a select directly into a hierarchical user type? Imagine
Using SQL Alchemy, I'd like to know if it's possible to get a list
Using the sound object .extract() method, it's possible to retrieve the uncompressed sound data

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.