I have data grouped by years. I would like to replace each data where x>(minimum+2) by NA. The minimum changes each year. I was thinking to extract the minimum by year using ddply, but i don't know how to compare each value from each year to their specific minimum...
Thanks a lot.
New <- Ancian %>%
group_by(Years) %>%
mutate_if(New$Data1, ~ replace(., . > (min(., na.rm = TRUE) + 2), NA))
I tried this, but it doesn't work... Also tried to do a "if" function after a group_by, but no results too...
group_by(Years) %>%
if(New$Data1 > (min(New$Data, na.rm = TRUE) + 2)) {
New$Data1 <- NA }
We can use mutate_if
after doing a grouping by 'years'
library(dplyr)
df1update <- df1 %>%
group_by(years) %>%
mutate_if(is.numeric, ~ replace(., . > (min(., na.rm = TRUE) + 2), NA))
If we need to do this only for a single variable
df1update <- df1 %>%
group_by(years) %>%
mutate(Data1 = replace(Data1, Data1 > (min(Data1, na.rm = TRUE) + 2), NA))