[R] "7:9, 12:14" in dataframe to c(7:9, 12:14)

From: Mikkel Grum <mi2kelgrum_at_yahoo.com>
Date: Mon 02 Jan 2006 - 18:27:00 EST


I want to do something like df[df$b %in% df2[i, 2], ] where df$b is a numeric vector and df2[i, 2] is a factor with
levels like "7:9, 12:14". For example:

a <- c(paste("A", 1:10, sep = ""), paste("B", 1:10, sep = ""))
b <- 1:20
df <- as.data.frame(cbind(a, b))
df$b <- as.numeric(levels(df$b))[as.integer(df$b)]

f <- c("X", "Y", "Z")
g <- c("1:6", "7:9, 12:14", "18")
df2 <- as.data.frame(cbind(f, g))

i <- 2
df[df$b %in% df2[i, 2], ] # or
df[df$b %in% levels(df2)[as.integer(df2[i, 2])], ] # this is the closest I've got

The results I want is given by
> df[df$b %in% c(7:9, 12:14), ]

    a b
7 A7 7
8 A8 8
9 A9 9
12 B2 12
13 B3 13
14 B4 14

Can it be done?

Mikkel Grum



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Mon Jan 02 18:33:04 2006

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:41:45 EST