Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a sequential list of letters with R

Tags:

r

alphabet

I would like to be able to create a sequence of letters in R (to assist in importing data from a SPSS file)

It's quite easy to create a sequence of numbers, for example:

seq(1,1000) [1] 1 2 3 4 5 6 ... 1000  paste("something_",1:12,sep="") [1] something1 something2 ... something12 

But is there any functionality for appending, pasting, or creating sequences of letters like this?

paste("This_",a:z,sep="") [1]This_a This_b This_c ... This_4z 

Thanks in advance!

like image 350
Brandon Bertelsen Avatar asked Sep 17 '09 15:09

Brandon Bertelsen


People also ask

How do you make a vector of letters in R?

To create a character vector in R we can enclose the vector values in double quotation marks but if we want to use a data frame values to create a character vector then as. character function can be used. For example, if we have a data frame df then all the values in the df can form a character vector using as.

How do you add sequential numbers in R?

The simplest way to create a sequence of numbers in R is by using the : operator. Type 1:20 to see how it works. That gave us every integer between (and including) 1 and 20 (an integer is a positive or negative counting number, including 0).


1 Answers

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" 
like image 107
Christopher DuBois Avatar answered Nov 15 '22 22:11

Christopher DuBois