[R] correct way to subset a vector

Juliet Hannah juliet.hannah at gmail.com
Thu Jul 9 17:40:34 CEST 2009


Hi,

#make example data
dat <- data.frame(matrix(rnorm(15),ncol=5))
colnames(dat) <- c("ab","cd","ef","gh","ij")

If I want to get a subset of the data for the middle 3 columns, and I
know the names of the start column and the end column, I can do this:

mysub <- subset(dat,select=c(cd:gh))

If I wanted to do this just on the column names, without subsetting
the data, how could I do this?

mynames <- colnames(dat);

#mynames
#[1] "ab" "cd" "ef" "gh" "ij"

Is there an easy way to create the vector c("cd","ef","gh") as I did
above using something similar to cd:gh?

Thanks,

Juliet




More information about the R-help mailing list