计算列中的句点数或星号数

时间:2015-12-15 19:52:41

标签: r count character

基本上我有一个名为XX$pitch_by_pitch的列,其中包含(F,S,B,X,。,*)等值,我试图计算此列中有多少个字符不是句号或星号。

样品:

pitch=data.frame(c(".CX",".BSCBS",".BSBX",".D",".FFB*B."))
names(pitch)[1]="pitch_by_pitch"
pitch$row=row.names(pitch)
pitch$pitch_by_pitch=as.character(pitch$pitch_by_pitch)
pitch$num=nchar(pitch$pitch_by_pitch)

显然最后一行计算所有字符,但我想删除句点和星号。

我尝试了以下内容:

count.slashes <- function(string)sum(unlist(strsplit(string, NULL)) == "*") 
within(x, Col2 <- vapply(Col1, count.slashes, 1)) 
pbp$pitchstar=which(unlist(strsplit(pbp$pitch_by_pitch,NULL)) == '*') 
pbp$pitchstar=str_count(pbp$pitch_by_pitch, "*")
pbp$star=length(which(pbp$pitch_by_pitch == '*'))

1 个答案:

答案 0 :(得分:2)

尝试pitch$num=nchar(gsub("\\*|\\.", "", pitch$pitch_by_pitch))

gsub("\\*|\\.", "", pitch$pitch_by_pitch)删除*列中的所有.pitch_by_pitch。然后使用nchar计算字符就足够了:

gsub("\\*|\\.", "", pitch$pitch_by_pitch)
# [1] "CX"    "BSCBS" "BSBX"  "D"     "FFBB"