* You are viewing Posts Tagged ‘text string’

Basic text string functions in R

To get the length of a text string (i.e. the number of characters in the string):

 nchar()

Using length() would just give you the length of the vector containing the string, which will be 1 if the string is just a single string.

To get the position of a regular expression match(es) in a text string x:

pos = regexpr('pattern', x) # Returns position of 1st match in a string
pos = gregexpr('pattern', x) # Returns positions of every match in a string

To get the position of a regular expression match in a vector x of text strings … Continue Reading