I have a bunch of nested lists that arrive with all character-typed elements. Some of these characters are in fact numbers that need to be converted to numeric-typed elements.
How can I modify these lists so that all possible character-typed numbers are converted to numeric, but the non-number character elements remain intact (e.g., aren't converted to NA by as.numeric())?
For example, I start with
raw_list <- list(
'a' = list('1', '2'),
'b' = list(
'c' = list('3', 'x'),
'd' = c('4', 'z'),
'e' = '5'))
But I need to get to
cleaned_list <- list(
'a' = list(1, 2),
'b' = list(
'c' = list(3, 'x'),
'd' = c('4', 'z'),
'e' = 5))
Bonus: Extra gratitudie for tidyverse/purrr-based solutions
Another option is to use
type.convert, which can take lists as arguments.This would be the same as using
purrrwithtype.convert.Output