J. Hermanson
J. Hermanson

Reputation: 33

Finding the position of a character within a string

I am trying to find the equivalent of the ANYALPHA SAS function in R. This function searches a character string for an alphabetic character, and returns the first position at which at which the character is found.

Example: looking at the following string '123456789A', the ANYALPHA function would return 10 since first alphabetic character is at position 10 in the string. I would like to replicate this function in R but have not been able to figure it out. I need to search for any alphabetic character regardless of case (i.e. [:alpha:])

Thanks for any help you can offer!

Upvotes: 3

Views: 446

Answers (1)

Pierre L
Pierre L

Reputation: 28441

Here's an anyalpha function. I added a few extra features. You can specify the maximum amount of matches you want in the n argument, it defaults to 1. You can also specify if you want the position or the value itself with value=TRUE:

anyalpha <- function(txt, n=1, value=FALSE) {
  txt <- as.character(txt)
  indx <- gregexpr("[[:alpha:]]", txt)[[1]]
  ret <- indx[1:(min(n, length(indx)))]

  if(value) {
    mapply(function(x,y) substr(txt, x, y), ret, ret)
    } else {ret}
}

#test
x <-  '123A56789BC'
anyalpha(x)
#[1] 4
anyalpha(x, 2)
#[1]  4 10
anyalpha(x, 2, value=TRUE)
#[1] "C" "A"

Upvotes: 2

Related Questions