How to filter list

Hello,
I have list object that includes three class objects: character, double, logical.
Can I divide this list into three lists according to class of object

Thanks

Hello @Lev_ani ,
you can do this with aid of the purrr package:

library(purrr)
a <- list('han','lev',0, 2, 3.14,FALSE,'Paris',TRUE) 
as <- list( char=a[purrr::map_lgl(a,is.character)],
            num=a[purrr::map_lgl(a,is.numeric)],
            log=a[purrr::map_lgl(a,is.logical)]  ) 
str(as)
#> List of 3
#>  $ char:List of 3
#>   ..$ : chr "han"
#>   ..$ : chr "lev"
#>   ..$ : chr "Paris"
#>  $ num :List of 3
#>   ..$ : num 0
#>   ..$ : num 2
#>   ..$ : num 3.14
#>  $ log :List of 2
#>   ..$ : logi FALSE
#>   ..$ : logi TRUE
Created on 2021-05-15 by the reprex package (v2.0.0)
1 Like

This topic was automatically closed 7 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.