Search code examples
regexrprefix

Matching an alphabet


I'm trying to use the grep function in R to see which elements in the vector I have contain a letter of the alphabet. I can do it by creating a vector of elements that contain a single letter, but it would be ridiculous to do this for all 27 letters. Below is something I've tried:

 count <- grep(pattern = "a", names(file))

Any advice would be appreciated!


Solution

  • Try this:

    alpha.in.file <- names(file)[grep(pattern='[[:alpha:]]', names(file))]
    

    This will allow you to extract the names of the file which contain only letters. To count them, simply

    length(alpha.in.file)