How do I convert everything in a nested list to characters?

I want to convert all the values in a nested list to a character class. I am currently using the following code:

list <- map(list, ~.x %>% mutate(across(.fns = as.character)))

But it does not appear to be converting. What should I change about the above line of code?

If you have a nested list, I suggest you use rapply() from base R. I invented some data that may, or may not, be similar to your data.

DATA <- list(1:3,list(7:9,c('a','s'),677),7:11)
DATA
#> [[1]]
#> [1] 1 2 3
#> 
#> [[2]]
#> [[2]][[1]]
#> [1] 7 8 9
#> 
#> [[2]][[2]]
#> [1] "a" "s"
#> 
#> [[2]][[3]]
#> [1] 677
#> 
#> 
#> [[3]]
#> [1]  7  8  9 10 11
rapply(DATA,as.character,how="replace")
#> [[1]]
#> [1] "1" "2" "3"
#> 
#> [[2]]
#> [[2]][[1]]
#> [1] "7" "8" "9"
#> 
#> [[2]][[2]]
#> [1] "a" "s"
#> 
#> [[2]][[3]]
#> [1] "677"
#> 
#> 
#> [[3]]
#> [1] "7"  "8"  "9"  "10" "11"

Created on 2022-07-30 by the reprex package (v2.0.1)

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.