2

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 Time as an example when only if Time matches the category, then look at column Value.

enter image description here

df <- read.table(header = TRUE, text = "SubjectID Treatment Time Value A1 Amutant T0 5.3 B0 Control T0 4.8 A3 Amutant T3 4 B1 Control T1 3 B3 Control T3 6.5 C2 Bmutant T2 2 C1 Bmutant T1 3") df %>% group_by (Time) %>% filter (Time == "T0") %>% filter (Value <5) 

This seems not what I exactly want to get because I want to subset the whole row of those that match T0 values <5.

The results should be filtering out only those subjects with T0 higher than 5 but should not affect T1, T2, T3.

Thanks in advance!

2
  • Maybe you're just looking for sth like this: df %>% filter (Time == "T0", Value <5) Commented Feb 12, 2020 at 13:52
  • But I need to only look at Value <5 for Time==“T0” or else it will filter also those from T2,T3 and T4. Commented Feb 12, 2020 at 14:37

3 Answers 3

2

If I understand you correctly You can use the subset function

subset(df, Time == "T0" & Value < 5 | Time != "T0") 

dplyr

df %>% filter(Time == "T0" & Value < 5 | Time != "T0") 
Sign up to request clarification or add additional context in comments.

4 Comments

Similar to my reply above, I need to only filter based on T0 that should not affect other time values.
The last statement could be simplified to df %>% filter(Time != "T0" | Value < 5)
Can you explain abit more in detail what goes wrong? Since the code now removes the values from T0 with values > 5, and the other values remain.
I think it did work looking at the dataset, thanks a lot!!! :)
2

It may be easiest to create a helper field that can be filtered on

library(dplyr) df %>% mutate(isFilter = case_when(Time == "T0" & Value > 5 ~ 1, TRUE ~ 0)) %>% filter(isFilter == 0) SubjectID Treatment Time Value isFilter 1 B0 Control T0 4.8 0 2 A3 Amutant T3 4.0 0 3 B1 Control T1 3.0 0 4 B3 Control T3 6.5 0 5 C2 Bmutant T2 2.0 0 6 C1 Bmutant T1 3.0 0 

1 Comment

If you're only testing one condition, you might as well use the base ifelse; Better yet: isFilter = as.numeric(Time == "T0" & Value > 5)
0

I think this will work.

dates <- rep( seq(as.numeric(as.Date("01-01-2020", format = "%d-%m-%Y")), as.numeric(as.Date("01-10-2020", format = "%d-%m-%Y"))), each = 24 ) value <- runif(length(dates), 1, 10) time <- runif(length(dates), 0, 1) data <- cbind(dates, value, time) data <- tibble::as_tibble(data) out <- data %>% filter(value != 0 & time > 5) isTRUE(sum(out$time < 5 | out$value == 0) == 0) #[1] TRUE 

!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.