I have a large list containing single lists of multiple tibbles. I want to select only relevant columns with purrr::map, but I get the error message that deplyr::select is not aplicable for lists. If my list only contains one tibble at a time the dplyr::select command works, as the select function recognizes the format as a tibble. So it should be quite easy to work around this error, but I can not quite get there.
The error I get is:
select()
doesn't handle lists
How can I work around this, as it should be possible to write a command something like that:
cleaned_lists <- map(lists,select,matches("string1|string2|string3|string4|string5"))
Thanks in advance!
CodePudding user response:
If it is a nested list
, we need an inner map
library(purrr)
library(dplyr)
cleaned_lists <- map(lists, map, select,
matches("string1|string2|string3|string4|string5"))
-Reproducible example
> lists <- list(list(mtcars, iris), list(mtcars, iris))
> map(lists, select, matches("Species|mpg"))
Error in `.f()`:
! `select()` doesn't handle lists.
Run `rlang::last_error()` to see where the error occurred.
> str(map(lists, map, select, matches("Species|mpg")))
List of 2
$ :List of 2
..$ :'data.frame': 32 obs. of 1 variable:
.. ..$ mpg: num [1:32] 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
..$ :'data.frame': 150 obs. of 1 variable:
.. ..$ Species: Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
$ :List of 2
..$ :'data.frame': 32 obs. of 1 variable:
.. ..$ mpg: num [1:32] 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
..$ :'data.frame': 150 obs. of 1 variable:
.. ..$ Species: Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...