dataframe subset
Bernhard Baumgartner wrote:
I have a dataframe with a column, say "x" consisting of values, each value appearing different times, e.g. x: 1,1,1,1,2,2,4,4,4,9,10,10,10,10,10 ... and a vector, including e.g.: y: 2,9,10,... I need a subset of the dataframe: all rows where x is equal to one of the values in y. Currently I use a loop for this, but because x and y are large this is very slow. Is there any idea how to solve this problem faster?
mydata <- data.frame(X = sample(1:10, 10000, replace=TRUE),
Y = sample(c(2,9,10), 10000, replace=TRUE))
newdata <- mydata[mydata$X %in% unique(mydata$Y),]
?"%in%"
Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 452-1424 (M, W, F) fax: (917) 438-0894