When unlisting unlist()
or flattening flatten()
a list with a factor variable, factors values are lost in the process. Additionally, when unlist()
everything is converted into "character". Which is the correct way to proceed? Below is my desired output. Thanks.
L <- list(x = structure(1L, .Label = c("Jujutsu", "Kaisen"), class = "factor"),
y = 2020,
z = "Shinjuku")
unlist()
unlist(L)
# x y z
# "1" "2020" "Shinjuku"
flatten()
flatten(L)
# $x
# [1] 1
#
# $y
# [1] 2020
#
# $z
# [1] "Shinjuku"
# $x
# [1] "Jujutsu"
#
# $y
# [1] 2020
#
# $z
# [1] "Shinjuku"
One option could be:
map(.x = L, ~ if(is.factor(.x)) as.character(.x) else .x)
$x
[1] "Jujutsu"
$y
[1] 2020
$z
[1] "Shinjuku"