I need to create a new column (insider_class) sorting data from a data.frame` based on specific rules using two columns as a reference.
I have a column with several parameters (parameter) and another with values (value).
The rule is:
If value pH >=6 and <=9 then insider_class=yes, if not then insider_class=no
If value DO >= 5.0 then insider_class=yes
I tried that, but some pH values don't respect the rule.
dput -->
df<-structure(list(Estacao2 = c("1", "1", "1", "1", "1", "1", "1",
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
"1", "1", "1", "1", "1", "1", "1", "1", "10", "10"), parameter = c("pH",
"DO", "pH", "DO", "pH", "DO", "pH", "DO", "pH", "DO", "pH", "DO",
"pH", "DO", "pH", "DO", "pH", "DO", "pH", "DO", "pH", "DO", "pH",
"DO", "pH", "DO", "pH", "DO", "pH", "DO"), value = c(4.475, 7.2,
5.65, 5.15, 6.65, 6.425, 6.4, 6.56, 6.05, 5.533, 5.75, 5.825,
5.625, 6.25, 5.833, 6.2, 5.35, 4.3, 5.867, 5.8, 5.375, 7.4, 5.6,
6.45, 5.55, 6.625, 6.033, 7.667, 7.438, 7.312)), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -30L), groups = structure(list(
Estacao2 = c("1", "10"), .rows = structure(list(1:28, 29:30), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -2L), .drop = TRUE))
code:
df2<-df%>%mutate(inside_class = case_when(
(parameter=='pH'& value %in% c(6.00:9.00) ~ 'yes'),
(parameter=='DO' & value>=5.0 ~'yes'),
TRUE~'no'
))
