Is there a nice way to convert a column of chars in a dataframe to a list in R Studio?
e.g.
convert type chr
"[1, 2, 3]"
"[11, 24, 3]"
"[1, 21, 3]"
"[14, 2, 31]"
to list
[1, 2, 3]
[11, 24, 3]
[1, 21, 3]
[14, 2, 31]
>Solution :
Those are not lists in R; they look like lists in python (language) and json (structure). We can capitalize on the latter:
vec <- c("[1, 2, 3]", "[11, 24, 3]", "[1, 21, 3]", "[14, 2, 31]")
jsonlite::stream_in(textConnection(paste(vec, collapse = "\n")),
simplifyDataFrame = FALSE, simplifyMatrix = FALSE)
# Imported 4 records. Simplifying...
# [[1]]
# [1] 1 2 3
# [[2]]
# [1] 11 24 3
# [[3]]
# [1] 1 21 3
# [[4]]
# [1] 14 2 31