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
