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!
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.
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).
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With