dplyr filter complex help

Dear dplyr gurus,

I am trying to perform a simple filtering using dplyr but it doesnt seem to work with what I want to do.

I want to filter a dataframe based on column B as an example when only if column B matches the category, then look at column C variable.

df
SubjectID Treatment Time Value
A1 Amutant T0 5.3
B0 control T0 4.8
A3 Bmutant T3 4
B1 control T1 3
B3 control T3 6

df %>%
group_by (Time) %>%
filter (Time == "T0") %>% #Should I use filter if or filter at here?
filter (Value <5)

This is not what I exactly want to get because I want to subset only based on "T0" values in "Value" and not filter out all the Time.

The results should be filtering out those subjects with T0 higher than 5.

Thanks in advance!

If you want to remove rows with Time == T0 and Value > 5, I think this code does what you want.

DF <- data.frame(Subject = c("A1", "B0", "A3", "B1", "B3"),
                 Treatment = c("Amutant", "control", "Bmutant", "control", "control"),
                 Time = c("T0", "T0", "T3", "T1", "T3"),
                 Value = c(5.3, 4.8, 4, 3, 6))
DF
#>   Subject Treatment Time Value
#> 1      A1   Amutant   T0   5.3
#> 2      B0   control   T0   4.8
#> 3      A3   Bmutant   T3   4.0
#> 4      B1   control   T1   3.0
#> 5      B3   control   T3   6.0
library(dplyr)

DF <- DF %>% filter(Time != "T0" | Value <= 5)
DF
#>   Subject Treatment Time Value
#> 1      B0   control   T0   4.8
#> 2      A3   Bmutant   T3   4.0
#> 3      B1   control   T1   3.0
#> 4      B3   control   T3   6.0

Created on 2020-02-12 by the reprex package (v0.2.1)

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