How to use letters instead of numbers within the LETTERS array?

Asked

Viewed 371 times

4

The vector LETTERS works as follows:

LETTERS[1:10]
[1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J"

Like adjust this vector to insert letters instead of numbers? Something like:

LETTERS[A:J]
Error: object 'A' not found

This would avoid having to count the letters of the alphabet to specify the desired character range.

  • 2

    LETTERS is a vector (object), not a function

  • 1

    Corrected. Thank you.

1 answer

6


A (long) option would be:

LETTERS[which("A" == LETTERS):which("J" == LETTERS)]
# [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J"

Within a function:

lettersSeq = function(primeira, segunda) {
  return(LETTERS[which(primeira == LETTERS):which(segunda == LETTERS)])
}

lettersSeq("A", "J")
# [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.