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

Dan Abner dan.abner99 at gmail.com
Wed Apr 26 02:14:43 CEST 2017


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 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 a third 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 a fourth example. List 1 1. Example 1. 2. Example 2. 10. Example
10. List 2 Example 1. 2. Example 2. These have been examples.")

text1

===

I would like the result to be c(5,5,5,5). Notice that sometimes there are
leading hard returns, other times not. Sometimes are there separate lists
and the same numbers are used in the enumerated items multiple times within
each character string. Sometimes the leading numbers for the enumerated
items exceed single digits. Notice that the delimiter may be ) or a period
(.). If the delimiter is a period and there are hard returns (example 2),
then I expect that will be easy enough to differentiate sentences ending
with a number from enumerated items. However, I imagine it would be much
more difficult to differentiate the two for example 4.

Any suggestions are appreciated.

Best,

Dan

	[[alternative HTML version deleted]]



More information about the R-help mailing list