I was wondering if it was possible to set the names of elements of a list at the end of a pipeline code.
data <- input_vars %>%
purrr::map(get_data)
names(data) <- input_vars
Currently I pass a string of variables into a function which retrieves a list of dataframes. Unfortunately this list does not automatically have named elements, but I add them "manually" afterwards. In order to improve readability I would like to have something as follows
data <- input_vars%>%
purrr::map(get_comm_data) %>%
setnames(input_vars)
However, this gives me the following error: Error in setnames(., input_vars) : x is not a data.table or data.frame
Does someone have an elegant solution to this?
Any help is appreciated!
setnames
if a fonction exported fromdata.table
package and you seem not to work withdata.table
. Base R function issetNames
and should work. You could also useset_names
frompurrr
package.?setnames
.