filter row using logical condation

Hi,
I have data frame & I want to filter two columns based on condition
df that have these variables (var1,var2,var3,var4)
absDiff <- abs(w_size - z_size) > 1 #this is my condition
I want to filter out rows where absDiff is greater than one
I tried doing this
filter(df,var2[absDiff)] but it didn't work

I know how to filter all with the condition
filter(df,abs(w_size - z_size) > 1)

but I want to filter only var2 and var3

UPDATE


df <- tibble(a= sample(1:100,1e6,replace=T),
             b= sample(1:100,1e6,replace=T),
             c= sample(1:100,1e6,replace=T),
             s= sample(1:100,1e6,replace=T),
             e= sample(1:100,1e6,replace=T),
             m= sample(1:100,1e6,replace=T),
             )


absCalc <-( df$a-df$c) >1

df %>% filter(df$s[absCalc]) 
%>%  filter(df$e[absCalc]) 

Hi!

To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:

Hi,
I updated my code I couldn't post the original data but I tried to post something similar to the data

What you want is not possible.

As far as I understand, you want to filter some rows from only two columns. That's not possile as tibble (or data.frame) doesn't allow columns of different lengths.

If you are OK with only two columns, then you can use df %>% select(s, e) %>% filter(absCalc). Or, you'll have to filter all columns using filter(df, absCalc).

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.