create a sequence between two letters

  • A+
Category:Languages

I want to create a sequence between two letters let's say "b" and "f". So the output is

"b" "c" "d" "e" "f" 

For numbers, we can do

2:6 #which gives output as  [1] 2 3 4 5 6 

Is there an easy way to do this with letters as well?

I have gone through Generate a sequence of characters from 'A'-'Z' but this produces all the letters and not sequence between specific letters.

My current solution is,

indx <- which(letters %in% c("b", "f"));  letters[indx[1] : indx[2]]  #[1] "b" "c" "d" "e" "f" 

This works but I am curious if there is an easy way to do this or a function in any of the package that I have missed?

EDIT - I do not want letters[2:6] as I do not know 2 and 6 beforehand. It could be between any two letters.

 


You can create your own function:

`%:%` <- function(l, r) {     intToUtf8(seq(utf8ToInt(l), utf8ToInt(r)), multiple = TRUE) } 

Usage:

"b" %:% "f" # [1] "b" "c" "d" "e" "f"  "f" %:% "b" # [1] "f" "e" "d" "c" "b"  "A" %:% "D" # [1] "A" "B" "C" "D" 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: