r - How to select rows from a data frame with replacement -


i have data frame defined follows:

t1 <- data.frame(x=c("a","b","c"),y=c(5,7,9))  > t1   x y 1 5 2 b 7 3 c 9 

and vector of picks:

picks <- c("b","c","b") 

how these rows, replacement, in order selected data frame? want:

x  y b  7 c  9 b  7 

i tried

> t1[t1$x %in% picks,]    x y 2 b 7 3 c 9 

and several other combinations of match, grep, which, etc , cannot out want. seems should easy i'm not finding path.

or can perform right join using data.table

library(data.table) picks <- data.table(x = picks) setdt(t1)[picks, on = "x"] #   x y #1: b 7 #2: c 9 #3: b 7 

by default merged data.table sorted according x in picks.


Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -