Creating a sequential list of letters with R
This is what you're looking for:
> paste("This_", letters, sep="")
> [1] "This_a" "This_b" "This_c" "This_d" "This_e" "This_f" "This_g" "This_h"
[9] "This_i" "This_j" "This_k" "This_l" "This_m" "This_n" "This_o" "This_p"
[17] "This_q" "This_r" "This_s" "This_t" "This_u" "This_v" "This_w" "This_x"
[25] "This_y" "This_z"
Creating a sequential list of numbers and letters with R
You could use outer
c(t(outer(paste0("var_", 1:2), LETTERS[1:4], paste0)))
#[1] "var_1A" "var_1B" "var_1C" "var_1D" "var_2A" "var_2B" "var_2C" "var_2D"
Or another option with paste0
and rep
paste0(rep(paste0("var_", 1:2), each = 4), LETTERS[1:4])
Generate a sequence of characters from 'A'-'Z'
Use LETTERS
and letters
(for uppercase and lowercase respectively).
Create a sequence between two letters
This would be another base R option:
letters[(letters >= "b") & (letters <= "f")]
# [1] "b" "c" "d" "e" "f"
R - generate a sequence of letters and numbers
You can do
paste0(rep(LETTERS[1:5], 3), rep(1:3, each = 5))
#> [1] "A1" "B1" "C1" "D1" "E1" "A2" "B2" "C2" "D2" "E2" "A3" "B3" "C3" "D3" "E3"
Create a list of sequential letters like in excel's header in R
In case if you are interested in base R solution, you can try this:
all <- expand.grid(LETTERS, LETTERS)
all <- all[order(all$Var1,all$Var2),]
out <- c(LETTERS, do.call('paste0',all))
The out will return 702 values as vectors, I believe you want to subset them until 559, so you can write: out[1:559].
To rename your columns you can use, where data_frame is your data frame name
names(data_frame) <- out[1:559]
One important note though, I am assuming here that you only wanted column with two characters not more than that.
A generic approach using gtools
comb <- lapply(1:3, function(x)gtools::permutations(26,x, LETTERS, repeats.allowed = TRUE))
## Using 3 for excel 3 combinations of alphabets
unlist(lapply(comb, function(x)do.call('paste0', data.frame(x,stringsAsFactors = FALSE))))
Some observations:
> out[1:50]
[1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K"
[12] "L" "M" "N" "O" "P" "Q" "R" "S" "T" "U" "V"
[23] "W" "X" "Y" "Z" "AA" "AB" "AC" "AD" "AE" "AF" "AG"
[34] "AH" "AI" "AJ" "AK" "AL" "AM" "AN" "AO" "AP" "AQ" "AR"
[45] "AS" "AT" "AU" "AV" "AW" "AX"
is there a way to extend LETTERS past 26 characters e.g., AA, AB, AC...?
Would 702 be enough?
LETTERS702 <- c(LETTERS, sapply(LETTERS, function(x) paste0(x, LETTERS)))
If not, how about 18,278?
MOAR_LETTERS <- function(n=2) {
n <- as.integer(n[1L])
if(!is.finite(n) || n < 2)
stop("'n' must be a length-1 integer >= 2")
res <- vector("list", n)
res[[1]] <- LETTERS
for(i in 2:n)
res[[i]] <- c(sapply(res[[i-1L]], function(y) paste0(y, LETTERS)))
unlist(res)
}
ml <- MOAR_LETTERS(3)
str(ml)
# chr [1:18278] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" ...
R: make column with a sequence of letters for all values in another column greater than threshold
We may use
library(data.table)
setDT(df)[number >= threshold, passed := letters[.I]]
create a sequence of numbers written in letters
You can use the english
package for this.
library(english)
as.english(100:1)
[1] one hundred ninety nine ninety eight ninety seven ...
Or for character output:
words(100:1)
[1] "one hundred" "ninety nine" "ninety eight" "ninety seven" ...
Edit: As neilfws notes words()
will produce character strings which might be more useful if you want to continue to process the data. as.english()
is a print/format method.
Related Topics
How to Know If R Is Running on 64 Bits Versus 32
R "Stats" Citation for a Scientific Paper
Most Mature Sparse Matrix Package for R
How to Add Chapter Bibliographies Using Bookdown
Defining Minimum Point Size in Ggplot2 - Geom_Point
Meaning of Band Width in Ggplot Geom_Smooth Lm
Writing Functions in R, Keeping Scoping in Mind
How to Knitr Markdown Straight Out of Your Workspace Using Rstudio
Obtain Latitude and Longitude from Address Without the Use of Google API
Insert Portions of a Markdown Document Inside Another Markdown Document Using Knitr
Really Fast Word Ngram Vectorization in R
Dplyr 'Rename' Standard Evaluation Function Not Working as Expected
Creating Tree Diagram for Showing Case Count Using R
R: Unexpected Results from P.Adjust (Fdr)
Raster Package Taking All Hard Drive