The mutate(listcol = map(listcol, part of this pattern is a lot of typing. It would be great if there was a more concise way to perform these types of map-in-place mutations – something like:
Sometimes it's possible to work around this by unnest()ing whatever is in listcol, but a) listcol sometimes contains something other than a tibble/data frame, b) that requires extra lines of code to nest and unnest and c) clashing column names or different table dimensions in the values of listcol can make this a pain.
Is there a function like this in purrr or elsewhere? Thanks!
There is a new function in dev version of tidyr called hoist that can be used for rectangling, but its use-case is to extract elements from a list, not to apply arbitrary function on each element.
As far as I know, there isn't such a function in tidyverse, but a) you can write a package for that or b) in your specific example, you can use purrr::compose and have only one mutate with all of the functions in one go (however, it's likely that your use-case is more general than this example).
Using two capabilities of pipe %>%: creating functions with . at the start and enabling lambda-expressions with {} (. servs as argument). For more information see this help page.
That said, I think there's something to be said for at least a little verbosity for this sort of code—it makes it much easier to read later. In particular, while sometimes you'll want to overwrite the old column, sometimes you'll want to add a new column, and seeing some assignment in there (albeit with =) makes the flow of data much easier to understand.