问题
I have a comma separated string in R:-
"a,b,c"
I want to convert it into a list which looks like this:
list("a","b","c")
How do I do that?
回答1:
This is a basic strsplit problem:
x <- "a,b,c"
as.list(strsplit(x, ",")[[1]])
# [[1]]
# [1] "a"
#
# [[2]]
# [1] "b"
#
# [[3]]
# [1] "c"
strsplit creates a list and the [[1]] selects the first list item (we only have one, in this case). The result at this point is just a regular character vector, but you want it in a list, so you can use as.list to get the form you want.
来源:https://stackoverflow.com/questions/24256044/comma-separated-string-to-list-in-r