Re: [R] Counting enumerated items in each element of a character vector

2017-04-26 Thread Boris Steipe
Let's be a bit careful. You'll probably need a regular expression. But maybe a regex can't work in principle, so one can't just gloss over the details. You said: "blah blah blah" can contain ANY text. If this is true, "blah blah blah" could contain the delimiters. If that is the case, a regex i

Re: [R] Counting enumerated items in each element of a character vector

2017-04-26 Thread Boris Steipe
What's the expected output for this sample? How do _you_ define what should be counted? > On Apr 26, 2017, at 8:33 AM, Dan Abner wrote: > > Hi all, > > I was not clearly enough in my example code. Please see below where "blah > blah blah" can be ANY text or numbers: No predictable pattern

Re: [R] Counting enumerated items in each element of a character vector

2017-04-26 Thread Dan Abner
Hi all, I was not clearly enough in my example code. Please see below where "blah blah blah" can be ANY text or numbers: No predictable pattern at all to what may or may not be written in place of "blah blah blah". text1<-c("blah blah blah. blah blah blah 1) blah blah blah 1 2) blah blah blah 10)

Re: [R] Counting enumerated items in each element of a character vector

2017-04-25 Thread Michael Hannon
Thanks, Ista. I thought there might be a "tidy" way to do this, but I hadn't use stringr. -- Mike On Tue, Apr 25, 2017 at 8:47 PM, Ista Zahn wrote: > stringr::str_count (and stringi::stri_count that it wraps) interpret > the pattern argument as a regular expression by default. > > Best, > Ista

Re: [R] Counting enumerated items in each element of a character vector

2017-04-25 Thread Ista Zahn
stringr::str_count (and stringi::stri_count that it wraps) interpret the pattern argument as a regular expression by default. Best, Ista On Tue, Apr 25, 2017 at 11:40 PM, Michael Hannon wrote: > I like Boris's "Hadley" solution. For the record, I've appended a > version that uses regular expres

Re: [R] Counting enumerated items in each element of a character vector

2017-04-25 Thread Michael Hannon
I like Boris's "Hadley" solution. For the record, I've appended a version that uses regular expressions, the only benefit of which is that it could be generalized to find more-complicated patterns. -- Mike counts <- sapply(text1, function(next_string) { loc_example <- length(gregexpr("Exampl

Re: [R] Counting enumerated items in each element of a character vector

2017-04-25 Thread Boris Steipe
I should add: there's a str_count() function in the stringr package. library(stringr) str_count(text1, "Example") # [1] 5 5 5 5 I guess that would be the neater solution. B. > On Apr 25, 2017, at 8:23 PM, Boris Steipe wrote: > > How about: > > unlist(lapply(strsplit(text1, "Example"), func

Re: [R] Counting enumerated items in each element of a character vector

2017-04-25 Thread Boris Steipe
How about: unlist(lapply(strsplit(text1, "Example"), function(x) { length(x) - 1 } )) Splitting your string on the five "Examples" in each gives six elements. length(x) - 1 is the number of matches. You can use any regex instead of "example" if you need to tweak what you are looking for. B.

[R] Counting enumerated items in each element of a character vector

2017-04-25 Thread Dan Abner
Hi all, I am looking for a streamlined way of counting the number of enumerated items are each element of a character vector. For example: text1<-c("This is an example. List 1 1) Example 1 2) Example 2 10) Example 10 List 2 1) Example 1 2) Example 2 These have been examples.","This is another ex