Is there an existing function for determining whether a row exists within a data frame? I suppose could do an apply/identical, but it seems like I'm missing something.
For example:
given such a data frame:
a b
1 1 cat
2 2 dog
Is there an existing function which will allow me to test whether the row (1, cat)
exists in the data frame?
Thanks, Zach
Try match_df
from plyr (using Marek's sample data):
library(plyr)
X <- data.frame(a=1:2, b=c("cat","dog"))
row_to_find <- data.frame(a=1, b="cat")
match_df(X, row_to_find)
For data from @Marek answer.
nrow(merge(row_to_find,X))>0 # TRUE if exists
Taking your example:
X <- data.frame(a=1:2, b=c("cat","dog"))
row_to_find <- data.frame(a=1, b="cat") # it has to be data.frame (not a vector) to hold different types
Then
duplicated(rbind(X, row_to_find))[nrow(X)+1]
gives you answer.
I suggest Ben Bolker's solution since nrow(merge(row_to_find,X))>0
solution doesn't work for me (always give TRUE) :
tail(duplicated(rbind(X,row_to_find)),1)>0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With