string 逗号分隔的字符串以在 r 中列出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24256044/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 02:18:53  来源:igfitidea点击:

comma separated string to list in r

stringrlistcomma

提问by umbersar

I have a comma separated string in R:-

我在 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?

我怎么做?

回答by A5C1D2H2I1M1N2O1R2T1

This is a basic strsplitproblem:

这是一个基本strsplit问题:

x <- "a,b,c"
as.list(strsplit(x, ",")[[1]])
# [[1]]
# [1] "a"
# 
# [[2]]
# [1] "b"
# 
# [[3]]
# [1] "c"

strsplitcreates a listand 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.listto get the form you want.

strsplit创建 alist[[1]]选择第一个列表项(在这种情况下我们只有一个)。此时的结果只是一个常规字符向量,但您希望将其放入 a 中list,因此您可以使用它as.list来获得您想要的形式。