I have the following list:
my_list = list(alpha = list('hi'),
beta = list(1:4, 'how'),
gamma = list(TRUE, FALSE, 'are'))
str(my_list)
List of 3
$ alpha:List of 1
..$ : chr "hi"
$ beta :List of 2
..$ : int [1:4] 1 2 3 4
..$ : chr "how"
$ gamma:List of 3
..$ : logi TRUE
..$ : logi FALSE
..$ : chr "are"
I would like to figure out which data types are contained within each level 1 element. To do this, I can use the following pipeline:
piped = map(my_list, ~map(., class) %>% unique %>% unlist)
str(piped)
List of 3
$ alpha: chr "character"
$ beta : chr [1:2] "integer" "character"
$ gamma: chr [1:2] "logical" "character"
...which works as expected. But when I try to nest the call to unique inside unlist(), I get something different:
composite = map(my_list, ~map(., class) %>% unlist(unique(.)))
str(composite)
List of 3
$ alpha: chr "character"
$ beta : chr [1:2] "integer" "character"
$ gamma: chr [1:3] "logical" "logical" "character"
Could someone please help me understand why these two approaches are not equivalent?
unique(.x)in place ofunique(.)but got the same result. - user51462