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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:27:23+00:00 2026-06-16T05:27:23+00:00

I am working with a large (millions of rows) data table with a list

  • 0

I am working with a large (millions of rows) data table with a list column containing deeply nested lists, which do not have uniform structure, size or order of elements (list(x=1,y=2) and list(y=2,x=1) may both be present and should be treated as identical). I need to repeatedly perform arbitrary groupings that include some columns from the data table as well as a subset of the data in the list column. Not all rows have values that will match the subset.

The approach I’ve come up with feels overly complicated. Here are the key points:

  • Identifying values in a nested list structure. My approach is to use ul <- unlist(list_col), which “flattens” nested data structures and builds hierarchical names for direct access to each element, e.g., address.country.code.

  • Ensuring that permutations of the same unlisted data are considered equal from a grouping standpoint. My approach is to order the unlisted vectors by the names of their values via ul[order(names(ul))] and assign the result as a new character vector column by reference.

  • Performing grouping on subsets of the flattened values. I was not able to get by= to work in any way with a column whose values are lists or vectors. Therefore, I had to find a way to map unique character vectors to simple values. I did this with digest.

Here are the two workhorse functions:

# Flatten list column in a data.table
flatten_list_col <- function(dt, col_name, flattened_col_name='props') {

  flatten_props <- function(d) {
    if (length(d) > 0) {
      ul <- unlist(d)
      names <- names(ul)
      if (length(names) > 0) {
        ul[order(names)]          
      } else {
        NA
      }
    } else {
      NA
    }
  }

  flattened <- lapply(dt[[col_name]], flatten_props)
  dt[, as.character(flattened_col_name) := list(flattened), with=F]
}

# Group by properties in a flattened list column
group_props <- function(prop_group, prop_col_name='props') {
  substitute({
    l <- lapply(eval(as.name(prop_col_name)), function(x) x[names(x) %in% prop_group])
    as.character(lapply(l, digest))
  }, list(prop_group=prop_group, prop_col_name=prop_col_name))
}

Here is a reproducible example:

library(data.table)

dt <- data.table(
  id=c(1,1,1,2,2,2), 
  count=c(1,1,2,2,3,3), 
  d=list(
    list(x=1, y=2), 
    list(y=2, x=1), 
    list(x=1, y=2, z=3),
    list(y=5, abc=list(a=1, b=2, c=3)),
    NA,
    NULL    
    )
)

flatten_list_col(dt, 'd')
dt[, list(total=sum(count)), by=list(id, eval(group_props(c('x', 'y'))))]

The output is:

> flatten_list_col(dt, 'd')
   id count      d   props
1:  1     1 <list>     1,2
2:  1     1 <list>     1,2
3:  1     2 <list>   1,2,3
4:  2     2 <list> 1,2,3,5
5:  2     3     NA      NA
6:  2     3             NA

> dt[, list(total=sum(count)), by=list(id, eval(group_props(c('x', 'y'))))]
   id                      group_props total
1:  1 325c6bbb2c33456d0301cf3909dd1572     4
2:  2 7aa1e567cd0d6920848d331d3e49fb7e     2
3:  2 ee7aa3b9ffe6bffdee83b6ecda90faac     6

This approach works but is pretty inefficient because of the need to flatten & order the lists and because of the need to calculate digests. I’m wondering about the following:

  1. Can this be done without having to create a flattened column by instead retrieving values directly from the list column? This will probably require specifying selected properties as expressions as opposed to simple names.

  2. Is there a way to get around the need for digest?

  • 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-16T05:27:25+00:00Added an answer on June 16, 2026 at 5:27 am

    There are a number of issues here. The most important (and one you haven’t come to yet due to others) is that that you are assigning by reference but trying to replace with more values than you have space to do so by reference.

    Take this very simple example

    DT <- data.table(x=1, y = list(1:5))
    DT[,new := unlist(y)]
    Warning message:
    In `[.data.table`(DT, , `:=`(new, unlist(y))) :
      Supplied 5 items to be assigned to 1 items of column 'new' (4 unused)
    

    You will lose all but the firstnrow(DT) items in the newly created list. They wont correspond to the rows of the data.table

    Therefore you will have to create a new data.table that will be large enough for you to explode these list variables. This won’t be possible by reference.

     newby <- dt[,list(x, props = as.character(unlist(data))), by = list(newby = seq_len(nrow(dt)))][,newby:=NULL]
    newby
    
    
       x props
     1: 1     1
     2: 1     2
     3: 1     2
     4: 1     1
     5: 1    10
     6: 2     1
     7: 2     2
     8: 2     3
     9: 2     5
    10: 2     1
    11: 2     2
    12: 2     3
    13: 3    NA
    14: 3    NA
    

    Note that as.character is required to ensure that all values are the same type, and a type that won’t lose data in the conversion. At the momemnt you have a logical NA value amongst lists of numeric / integer data.


    Another edit to force all components to be character (even the NA). props is now a list with 1 character vector for each row.

    flatten_props <- function(data) {
    if (is.list(data)){
    ul <- unlist(data)
    if (length(ul) > 1) {
    ul <- ul[order(names(ul))]
    }
    as.character(ul) } else {
    as.character(unlist(data))}}

    dt[, props := lapply(data, flatten_props)]
    dt
       x   data   props
    1: 1 <list>     1,2
    2: 1 <list>  10,1,2
    3: 2 <list>   1,2,3
    4: 2 <list> 1,2,3,5
    5: 3     NA      NA
    6: 3   
    
    dt[,lapply(props,class)]
              V1        V2        V3        V4        V5        V6
    1: character character character character character character
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working with a large amount of data: 6 million rows. I need
I'm working on a large project (for me) which will have many classes and
I am working with a very large data set which I am downloading from
I am working with a rather large mysql database (several million rows) with a
I have a large amount of data stored in an XML file, 173 MB
I have a fairly simple table with approx a million rows. id | my_col
I have several large (30+ million lines) text databases which I am cleaning up
I have a Rails app that processes a large (millions) number of records in
I am working with large datasets (10s of millions of records, at times, 100s
I'm working no a site which stores individual page views in a 'views' table:

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.