I have a list object that contains several elements. One element is a data frame that I wish to modify: I want to perform some operations such as column renaming and mutating new columns.
Although one simple way is to extract the nested data frame, modify it, and finally re-assign the output to the original parent list, I'd like to avoid such solution because it requires intermediate assignment.
Example
Data. let's build a list of several data objects
library(tibble)
my_list <- lst(letters, mtcars, co2, uspop, iris)
Task.
I want to modify
my_list$mtcarsto:- rename the
cylcolumn - compute a new column that takes a square root of values in
mpgcolumn
- rename the
I want to modify
my_list$iristo:- select columns that start with
sepal - rename them to lowercase
and ultimately I expect to get back a list object that is identical to the original
my_list, except for the changes I made formtcarsandiris.- select columns that start with
My attempt. Right now, the only way I know to achieve this involves re-assignment:
library(dplyr)
my_list$mtcars <-
my_list$mtcars %>%
rename("Number of cylinders" = cyl) %>%
mutate(sqrt_of_mpg = sqrt(mpg))
my_list$iris <-
my_list$iris %>%
select(starts_with("Sepal")) %>%
rename_with(tolower)
My question: Given my_list, how could I point to a nested element by its name, specify which actions should happen to modify it, and get back the parent my_list with just those modifications?
I imagine some sort of a pipe that looks like this (just to get my general idea)
## DEMO ##
my_list %>%
update_element(which = "mtcars", what = rename, mutate) %>%
update_element(which = "iris", what = select, rename)
Thanks!