Learn R Programming

lessR (version 2.5)

to: Create a Sequence of Numbered Variable Names with a Common Prefix

Description

Generates sequentially numbered variable names, all starting with the same prefix, usually in conjunction with reading data values into R.

Usage

to(prefix, until, from=1, same.size=TRUE)

Arguments

prefix
Character string that begins each variable name.
until
Last number in the sequence.
from
First number in the sequence.
same.size
If TRUE, pads the beginning of each number for the variable name with leading zeros so that all names are of the same width.

Details

Some data sets, particularly those from surveys, have sequentially numbered variable names, each beginning with the same prefix, such as the first later of the name of a set of related attitude items. This function generates the string of such variable names, generally intended for use in a read statement for reading the data and then naming the variables, or for a subsequent assignment of the names with a names. Relies upon the R paste function.

See Also

paste.

Examples

Run this code
# generate: "m01" "m02" "m03" "m04" "m05" "m06" "m07" "m08" "m09" "m10"
to("m", 10)

# generate: "m1"  "m2"  "m3"  "m4"  "m5"  "m6"  "m7"  "m8"  "m9"  "m10"
to("m",10, same.size=FALSE)

# generate a 10 x 10 data frame
mydata <- data.frame(matrix(rnorm(100), nrow=10))
# name the variables in the data frame
names(mydata) <- to("m", 10)

Run the code above in your browser using DataLab