Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a sequence between two letters

Tags:

r

character

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?

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

like image 607
Ronak Shah Avatar asked Nov 26 '18 08:11

Ronak Shah


People also ask

How do you write a sequence of letters in R?

To create a sequential uppercase alphabet in R, use the LETTERS constant. The LETTERS is a character constant in R that generates an uppercase alphabet, and you can use it with different functions to extract the result as per your requirement.


3 Answers

This would be another base R option:

letters[(letters >= "b") & (letters <= "f")]
# [1] "b" "c" "d" "e" "f"
like image 143
r.user.05apr Avatar answered Oct 24 '22 03:10

r.user.05apr


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"
like image 28
Sven Hohenstein Avatar answered Oct 24 '22 01:10

Sven Hohenstein


Another option with match, seq and do.call:

letters[do.call(seq, as.list(match(c("b","f"), letters)))]

which gives:

[1] "b" "c" "d" "e" "f"

Making a function of this such that it works with both lower-case and upper-case letters:

char_seq <- function(lets) {
  switch(all(grepl("[[:upper:]]", lets)) + 1L,
         letters[do.call(seq, as.list(match(lets, letters)))],
         LETTERS[do.call(seq, as.list(match(lets, LETTERS)))])
}

the output of this:

> char_seq(c("b","f"))
[1] "b" "c" "d" "e" "f"

> char_seq(c("B","F"))
[1] "B" "C" "D" "E" "F"

This function can be extended with checks on the correctness of the input:

char_seq <- function(lets) {
  g <- grepl("[[:upper:]]", lets)
  if(length(g) != 2) stop("Input is not of length 2")
  if(sum(g) == 1) stop("Input does not have all lower-case or all upper-case letters")
  switch(all(g) + 1L,
         letters[do.call(seq, as.list(match(lets, letters)))],
         LETTERS[do.call(seq, as.list(match(lets, LETTERS)))])
}

resulting in proper error-messages when the input is not correct:

> char_seq(c("B"))
Error in char_seq(c("B")) : Input is not of length 2

> char_seq(c("b","F"))
Error in char_seq(c("b", "F")) : 
  Input does not have all lower-case or all upper-case letters
like image 17
Jaap Avatar answered Oct 24 '22 03:10

Jaap