[R] Subset using grepl

Kang Min ngokangmin at gmail.com
Sat Jan 29 09:46:04 CET 2011


Hi all,

I would like to subset a dataframe by using part of the level name.

x <- rep(LETTERS[1:20],3)
y <- rep(1:3, 20)
z <- paste(x,y, sep="")
random.data <- rnorm(60)
data <- as.data.frame(cbind(z, random.data))

I need rows that contain the letters A to J, so I tried:

subset(data, grepl(LETTERS[1:10], z)) # got only rows with A
subset(data, z %in% LETTERS[1:10]) # got no rows

I think I'm getting close to the solution but need a little bit of
help here, thanks in advance.

Kang Min



More information about the R-help mailing list