逗号分隔的字符串以在r中列出 [英] comma separated string to list in r

查看:60
本文介绍了逗号分隔的字符串以在r中列出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在R中有一个逗号分隔的字符串:-

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")

我该怎么做?

推荐答案

这是一个基本的strsplit问题:

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

strsplit创建一个list,然后[[1]]选择第一个列表项(在这种情况下,我们只有一个).此时的结果只是一个常规的字符向量,但是您希望在list中使用它,因此可以使用as.list来获取所需的形式.

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.

这篇关于逗号分隔的字符串以在r中列出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆