Jump to content

Looking for something like REGEXP_COUNT function


Todd Holman 2

Recommended Posts

I think there is no built-in function for this.

 

But you can create your own function using base R following this discussion:

 

https://stackoverflow.com/questions/56759358/how-to-count-string-occurrences-in-another-string-in-base-r

 

Or using str_count() from stringr package. 

 

Where is your "specific text character" coming from Some user input 

Link to comment
Share on other sites

Now I had time to do at least the simple stringr solution.

 

Your expression function, let's name it StringCount, should look like this:

 

StringCount <- function(col, pattern){
   return(stringr::str_count(col, pattern))
}
output <- StringCount(pattern = input1, col = input2)

 

After you have registered this expression function you can use it for a calculated column:

 

StringCount(your.pattern, [your.column]])

 

For example: StringCount(":", [ColA])

 

One more edit to this post:

 

If you don't want to create an expression function you can use the following directly in your calculated column:

 

TERR_Integer(
"output <- as.integer(stringr::str_count(input1, input2))",[your.column],"your.pattern"
)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...