在字符串中用逗号分隔的计数值 [英] Count values separated by a comma in a character string
问题描述
我有这个示例数据
d < - 30,3
class(d)
我在工作数据框架的一列中有这个字符对象,我需要能够识别多少
我试图使用 length(d)
,但它说1
在这里寻找解决方案后,我尝试了
eval(parse(text = d))
as.numeric(d)
as.vector.character(d)
$ b
这两种方法都是短的,对字符串的向量进行工作,不涉及显式构造拆分字符串的费用,不使用任何包。这里 d
是诸如 d <-c(1,2,3,5,2)等字符串的向量, code>:
1)count.fields
count.fields(textConnection(d),sep =,)
2)gregexpr
sapply(gregexpr(,,d)
I have this example data
d<-"30,3"
class(d)
I have this character objects in one column in my work data frame and I need to be able to identify how many numbers it has.
I have tried to use length(d)
, but it says 1
After looking for solution here I have tried
eval(parse(text='d'))
as.numeric(d)
as.vector.character(d)
But it still doesn't work.
Any straightforward approach to solve this problem?
These two approaches are each short, work on vectors of strings, do not involve the expense of explicitly constructing the split string and do not use any packages. Here d
is a vector of strings such as d <- c("1,2,3", "5,2")
:
1) count.fields
count.fields(textConnection(d), sep = ",")
2) gregexpr
sapply(gregexpr(",", d), length) + 1
这篇关于在字符串中用逗号分隔的计数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!