Home > Net >  Is there a way in R to count the number of substrings in a string enclosed in square brackets, all s
Is there a way in R to count the number of substrings in a string enclosed in square brackets, all s

Time:09-26

['ax', 'byc', 'crm', 'dop']

This is a character string, and I want a count of all substrings, ie 4 here as output. Want to do this for the entire column containing such strings.

CodePudding user response:

We may use str_count

library(stringr)
str_count(str1, "\\w ")
[1] 4

Or may also extract the alpha numeric characters into a list and get the lengths

lengths(str_extract_all(str1, "[[:alnum:]] "))

If it is a data.frame column, extract the column as a vector and apply str_count

str_count(df1$str1, "\\w ")

data

 str1 <- "['ax', 'byc', 'crm', 'dop']"
df1 <- data.frame(str1)

CodePudding user response:

Here are a few base R approaches. We use the 2 row input defined reproducibly in the Note at the end.

lengths(strsplit(DF$b, ","))
## [1] 4 4

nchar(gsub("[^,]", "", DF$b))   1
## [1] 4 4

count.fields(textConnection(DF$b), ",")
## [1] 4 4

Note

DF <- data.frame(a = 1:2, b = "['ax', 'byc', 'crm', 'dop']")
  • Related