create lists of nested items in r

You can do it using split, which creates lists based on the provided function. Here I just introduced another column using the years and named them to according to your preference. Then, split the rest using only the column names.

df_ALL$split <- ifelse(df_ALL$YEAR > 2016, "2017-2019", "2014-2016")

nested_list <- lapply(split(df_ALL, df_ALL$split), function(x) {
  y <- split(x, x$SEASON)
  return(lapply(y, function(x) {
    split(x, x$COMPANY)

Created on 2020-11-05 by the reprex package (v0.3.0)

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top