Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find rows where multiple columns have the same value

library(tidyverse)
d = data.frame(x=c('A','B','C'), y=c('A','B','D'), z=c('X','B','C'), a=1:3)
print(d)
  x y z a
1 A A X 1
2 B B B 2
3 C D C 3
d %>% filter(x==y) # Returns rows 1 and 2
d %>% filter(x==z) # Returns rows 2 and 3
d %>% filter(x==y & x==z) # Returns row 2

How can I do what the very last line is doing with more concise syntax for some arbitrary set of columns? For example, filter(all.equal(x,y,z)) which doesn't work but expresses the idea.

like image 356
Ben S. Avatar asked Oct 29 '25 01:10

Ben S.


2 Answers

With comparisons, on multiple columns, an easier option is to take one column out (x), while keeping the rest by looping in if_all, then do the ==, so that it will return only TRUE when all the comparisons for that particular row is TRUE

library(dplyr)
d %>% 
  filter(if_all(y:z, ~ x == .x))
like image 157
akrun Avatar answered Oct 30 '25 17:10

akrun


Same idea, with across instead of if_all;

d %>% 
  filter(across(y:z,  ~`==`(.x, x)))
like image 44
M-- Avatar answered Oct 30 '25 16:10

M--



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!