How to use modify_in to mutate all list elements to numerics?

library(purrr)

mm <- list(a = list("1", "2"), b = list("3", "4"), c = list("5", "6"))
nn <- list(a = list("7", "8"), b = list("9", "10"), c = list("11", "12"))

We planned to change all elements to numeric,
the following codes didn't work,

kk <- list(mm, nn)
kk |> map(as.numeric)
kk |> modify_in(as.numeric)
kk |> map(modify_in, as.numeric)
kk |> map(modify_in, ~ as.numeric)

How to fix it, thanks

This makes your data numeric. However the structures of input and output are not identical :

library(purrr)

mm <- list(a = list("1", "2"), b = list("3", "4"), c = list("5", "6"))
nn <- list(a = list("7", "8"), b = list("9", "10"), c = list("11", "12"))

kk <- list(mm, nn)

str(kk)
#> List of 2
#>  $ :List of 3
#>   ..$ a:List of 2
#>   .. ..$ : chr "1"
#>   .. ..$ : chr "2"
#>   ..$ b:List of 2
#>   .. ..$ : chr "3"
#>   .. ..$ : chr "4"
#>   ..$ c:List of 2
#>   .. ..$ : chr "5"
#>   .. ..$ : chr "6"
#>  $ :List of 3
#>   ..$ a:List of 2
#>   .. ..$ : chr "7"
#>   .. ..$ : chr "8"
#>   ..$ b:List of 2
#>   .. ..$ : chr "9"
#>   .. ..$ : chr "10"
#>   ..$ c:List of 2
#>   .. ..$ : chr "11"
#>   .. ..$ : chr "12"
str(map(kk,~map(.,as.numeric)))
#> List of 2
#>  $ :List of 3
#>   ..$ a: num [1:2] 1 2
#>   ..$ b: num [1:2] 3 4
#>   ..$ c: num [1:2] 5 6
#>  $ :List of 3
#>   ..$ a: num [1:2] 7 8
#>   ..$ b: num [1:2] 9 10
#>   ..$ c: num [1:2] 11 12
Created on 2021-09-20 by the reprex package (v2.0.0)

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.