Existing function to view if a row exists in a data frame?

Is there an existing function to determine if a row exists in a data frame? I suppose I could apply / identical, but it looks like I missed something.

For example:

for such a data frame:

ab 1 1 cat 2 2 dog 

Is there an existing function that allows me to check if the string (1, cat) exists in the data frame?

Thanks Zach

+7
source share
6 answers

For data from @Marek's answer.

 nrow(merge(row_to_find,X))>0 # TRUE if exists 
+4
source

Try match_df from plyr (using Marek 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) 
+16
source

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 an answer.

+6
source

I propose a solution to Ben Bolker , since the solution nrow(merge(row_to_find,X))>0 does not work for me (always let's TRUE):

 tail(duplicated(rbind(X,row_to_find)),1)>0 
+1
source

For a vector y with the same number of elements as the columns in the dataframe, dfrm:

 apply(dfrm, 1, function(x) all( x == y) ) 

Should return a vector of TRUE and FALSE, which, in turn, can be used as an index in [,]

 dfrm[ apply(dfrm, 1, function(x) all( x == y) ) , ] 

The identical function is probably too strict, as it also checks for attributes.

 > y=c(1,2,3) > x = data.frame(a=1:10, b=2:11, c=3:12) > identical(x[1,] , y) [1] FALSE 
0
source

For dplyr and tidyverse you can use dplyr:anti_join() . According to his documentation, dplyr::anti_join(x, y) "returns all rows from x , where y does not have corresponding values, preserving only columns from x ." Therefore, for dplyr::anti_join(row, df) result has zero rows, then row really was in df , if it has one row, then row not in df .

 library(dplyr) df <- tribble(~a, ~b, 1, "cat", 2, "dog") #> # A tibble: 2 x 2 #> ab #> <dbl> <chr> #> 1 1.00 cat #> 2 2.00 dog row <- tibble(a = 1, b = "cat") #> # A tibble: 1 x 2 #> ab #> <dbl> <chr> #> 1 1.00 cat nrow(anti_join(row, df)) == 0 # row is in df so should be TRUE #> Joining, by = c("a", "b") #> [1] TRUE row <- tibble(a = 3, b = "horse") #> # A tibble: 1 x 2 #> ab #> <dbl> <chr> #> 1 3.00 horse nrow(anti_join(row, df)) == 0 # row is not in df so should be FALSE #> Joining, by = c("a", "b") #> [1] FALSE 
0
source

All Articles