Purrr package - R studio

I am struggling with finding a code which allows me to create a named list containing 3 elements which are functions.
For the purpose of this question, it is necessary to download the purrr package.

the 3 functions, defined as elements of a named list, to be included are the following :
linear = function(x) x,
quadratic = function(x) (x - pi/2)^2 - pi/2
sinusoidal = function(x) sin(x)

What I have tried so far :

library(purrr)
list_fun <- list(x)
map(list_fun, linear:x, quadratic:(x - pi/2)^2 - pi/2, sinusoidal:sin(x))

library(tidyverse)
linear <- function(x) x
quadratic <- function(x) (x - pi/2)^2 - pi/2
sinusoidal <- function(x) sin(x)
(mylist <- c(linear,quadratic,sinusoidal) %>% 
    purrr::set_names(c("linear","quadratic","sinusoidal")) %>%
    purrr::map(~.))

but it seems a somewhat artificial requirement, given that we start with a vector of functions, and go to a named list, why go through the effort, why not a named vector ?

(myvec <- c(linear,quadratic,sinusoidal) %>% 
    purrr::set_names(c("linear","quadratic","sinusoidal")))

Thank you very much it worked!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.