In R there are many times where we will work with lists. I won’t go into why lists are great or really the structure of a list but rather simply working with them.
Now let’s look at somethings we can do with lists. First, let’s see if we can get the class of each item in the list. We are going to use lapply() for this.
Now, let’s perform some simple operations on each item of the list.
lapply(l, length)
[[1]]
[1] 26
[[2]]
[1] 26
[[3]]
[1] 26
try(lapply(l, sum))
Error in FUN(X[[i]], ...) : invalid 'type' (character) of argument
Ok so we see taking the sum of the first element of the list in lapply() did not work because of a class type mismatch. Let’s see how we can get around this an only apply the sum function to a numeric type. To do this we can rely on {purrr} by using a function map_if()