Comparison of scalars in dplyr filter

In the following reproducible example, I want to filter the lines in df, where the expression of the variables gives a specific result. Lines 1 and 3 meet the requirements and must be returned. However, my first attempt was incorrect, as I used ==to compare scalars.

df <- data.frame(matrix(c(1,8,3,7,4,5,6,2,9,1,2,3,4,5,6,7,8,9,9,6,4,3,5,8,1,7,2),ncol=9,byrow=T))

df %>%
    filter(X1+13*X2/X3+X4+12*X5-X6-11+X7*X8/X9-10==66)

  X1 X2 X3 X4 X5 X6 X7 X8 X9
1  9  6  4  3  5  8  1  7  2

I tried to fix my mistake using the isTRUE (all.equal (...)) method, but, to my surprise, I did not get any results at all.

df %>%
    filter(isTRUE(all.equal(X1+13*X2/X3+X4+12*X5-X6-11+X7*X8/X9-10,66))) 

[1] X1 X2 X3 X4 X5 X6 X7 X8 X9
<0 rows> (or 0-length row.names)

How do I make this comparison?

+4
source share
2 answers

all.equal not vectorized - why not

df %>% 
    filter(abs(X1+13*X2/X3+X4+12*X5-X6-11+X7*X8/X9-10 - 66) < 1e-8)
+5
source

isTRUE returns a logical length vector and so your second statement is equivalent

df %>% filter(FALSE)

, , , . - , , , .

df %>% 
  # calculate condition
  mutate(value = X1+13*X2/X3+X4+12*X5-X6-11+X7*X8/X9-10, 
         cond = sapply(value, function(x) isTRUE(all.equal(x, 66)))) %>%
  # filter
  filter(cond) %>%
  # remove unnecessary values
  mutate(value = NULL, cond = NULL)
##   X1 X2 X3 X4 X5 X6 X7 X8 X9
## 1  1  8  3  7  4  5  6  2  9
## 2  9  6  4  3  5  8  1  7  2
+1

Source: https://habr.com/ru/post/1589201/


All Articles