double match a data.frame
I'd like to do a double match on a data.frame to extract row indices. For example, in the following data.frame, I want to match first on match1, and within each element of match1, I want to match by开发者_运维知识库 match2. Think of this as a double-sort (first by name, then by date) except that I'm not sorting, I'm matching
test = data.frame( name = c( "bob" , "jane","adam","jane" ,"bob" , "adam" ) , date = as.Date(c(1,2,2,1,2,1)))
match1 = c( "jane" , "adam", "bob")
match2 = c( as.Date(c(2,1)))
result = c( 2,4,3,6,5,1 )
Note that for your code to work as written, you need the zoo
library loaded (it masks as.Date
)
library("zoo")
It seem what you want it the indicies of the data.frame that correspond to the data.frame sorted by name
according to match1
and then by date
according to match2
. You can use factors to create this ordering, and then order
to get the indicies.
order(factor(test$name, match1), factor(as.character(test$date), as.character(match2)))
The as.character
is needed to coerce the dates to characters so that they can be turned into factors with the right ordering.
One way to check for a double-match is to do a double-sort on "test" (call this test2) and test for the equality of "test" and "test2"
精彩评论