my_vector <- "'ABC', 'DEF','GHI', '123'"
Given the above vector, how can I find the number of elements separated by a comma? In this case, I want to treat, for example, 'ABC'
as an element. So, there are four elements in the given vector.
my_vector <- "'ABC', 'DEF','GHI', '123'"
Given the above vector, how can I find the number of elements separated by a comma? In this case, I want to treat, for example, 'ABC'
as an element. So, there are four elements in the given vector.
The number of elements will be one more than the number of commas counted by the str_count() function from the stringr package.
my_vector <- "'ABC', 'DEF','GHI', '123'"
library(stringr)
str_count(my_vector, ",")
[1] 3
Complementing @FJCC answer, you can also try to describe the elements directly if they follow a well defined pattern.
my_vector <- "'ABC', 'DEF','GHI', '123'"
library(stringr)
str_count(my_vector, "'.+?'")
#> [1] 4
Created on 2022-07-04 by the reprex package (v2.0.1)
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.
If you have a query related to it or one of the replies, start a new topic and refer back with a link.