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

Mikkel Grum mi2kelgrum at yahoo.com
Mon Jan 2 08:27:00 CET 2006


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




More information about the R-help mailing list