## You are not using the word "factor" as it is used in standard S
## language terminology. I translate your question as follows:
##
## I have a dataframe y with 124 variables. I would like to create a
## subset of rows of this dataframe by selecting a number of levels of
## the data.frame factor "HOSP". The levels I wish to use in the subset
## can be found in a vector x.
##
## If I have interpreted your question correctly, here is the answer.
##
## If this is not what you want then please resubmit your request to the
## list with a simple executable set of S statements for a smaller
## example. Show the y data.frame including the factor HOSP. Give the
## vector x. Show us exactly what you expect to see in your result.
y <- data.frame(aa=rnorm(12),
bb=factor(rep(letters[1:3],4)),
HOSP=factor(rep(LETTERS[1:4],3)),
dd=factor(sample(letters[1:5], 12, replace=T)))
x <- c("A","B")
subset.rows <- (match(y$HOSP, x, 0) > 0)
result <- y[subset.rows, ]
result
|