0

I have several data frames of ordered and factor variables. I want to create a new data frames of numeric variables from the ordered variables. I wish to store all data frames in a hierarchical list structure.

I was wondering if there is a simple one-step way to use a list element to create the next element?

A reproducible example of a two-step method that produces the desired output:

library(dplyr)

# Step 1: Create list with first element
my_list <- list(
    ord = data.frame(
        a1 = letters[1:3],
        a2 = ordered(1:3)
    )
)

# Step 2: Add second element
my_list$cont <- mutate(
    my_list$ord, # I want to use the previous list element "ord" to create the next
    across(-a1, as.numeric)
)

# The desired output:
my_list

> $ord
     A data.frame: 3 × 2
     a1    a2
     <chr> <ord>
     a     1
     b     2
     c     3
> $cont
     A data.frame: 3 × 2
     a1    a2
     <chr> <dbl>
     a     1
     b     2
     c     3

The working example above seem pretty forward, but add more list items and more manipulations of the dataframes this way, and it will soon get more complicated.

That's why I was wondering if there is a simpler way, such as passing the previous list item to the next when the list is created. Below is a pseudo-code example for a one-step method for what I want to achieve:

library(dplyr)

my_list <- list(
    ord = data.frame(
        a1 = letters[1:3],
        a2 = ordered(1:3)
    ),
    cont = mutate(
        my_list$ord, # I want to use the previous list element "ord" to create the next
        across(-a1, as.numeric)
    )
)

my_list
Pål Bjartan
  • 793
  • 1
  • 6
  • 18
  • It's easier to help you if you provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick May 25 '22 at 14:05
  • Thanks or the input. I've now added a reproducible example that will produce the desired output. I also made a correction to the pseudo-code example to better clarify what I want to achieve. – Pål Bjartan May 26 '22 at 10:51
  • 1
    You can do this with `lst()` instead of `list()`. Then in your pseudocode you can remove `my_list$` and refer directly to the components you already created (i.e. `ord`). – lhs May 26 '22 at 13:05
  • Thanks, that's certainly easy and useful! If you post it as an answer, I'll tick it off as solved. Is there any differences in behavior between `lst()` and `list()` that are likely to cause issues? – Pål Bjartan May 26 '22 at 20:13

1 Answers1

0

Based on lhs' comment, the solution is actually fairly easy using lst() instead of list() to create the list. From lst()'s R Help page: "lst() builds components sequentially. When defining a component, you can refer to components created earlier in the call."

Working code example:

library(dplyr) # (Imported from "tibble")

my_list <- lst(
    ord = data.frame(
        a1 = letters[1:3],
        a2 = ordered(1:3)
    ),
    cont = mutate(
        ord, # Using previous list component 
        across(-a1, as.numeric)
    )
)

my_list
Pål Bjartan
  • 793
  • 1
  • 6
  • 18