Todd Holman 2 Posted August 2, 2021 Share Posted August 2, 2021 I have a column that I need to count the occurances of a specific text character and then create another column with that count. Perhaps I am missing something simple Link to comment Share on other sites More sharing options...
Fabian Duerr Posted August 2, 2021 Share Posted August 2, 2021 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 More sharing options...
Todd Holman 2 Posted August 2, 2021 Author Share Posted August 2, 2021 Thanks I'll look into your suggestions. The specific text char is always the same ":" It is a deleination between updates. So I am looking for how many updated have occured in this field. Thanks Todd Link to comment Share on other sites More sharing options...
Todd Holman 2 Posted August 3, 2021 Author Share Posted August 3, 2021 Wow, Great to know. I got it working following your first suggestion. I had never ventured into this aspect of Spotfire so your examples are extreamly helpful! Thanks! Todd Link to comment Share on other sites More sharing options...
Fabian Duerr Posted August 3, 2021 Share Posted August 3, 2021 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now