Let's consider markers
with their coefficient of variation (cv
) and three reference cv (rcv
):
Initial data:
marker cv rcv1 rcv2 rcv3
<chr> <dbl> <dbl> <dbl> <dbl>
1 AAA 7 10 8 5
2 BBB 4 5 3 1
3 CCC 11 20 15 12
4 DDD 8 7 5 2
I would like to mutate three new variables:
rcv_value
: the closestrcv
value greater than thecv
rcv_name
: the column name of thatrcv_value
cv_conclusion
:- ok if the
cv
is lower than one or the other of thercvs
- ko if the
cv
is higher than the highestrcv
- ok if the
Desired output:
marker cv rcv1 rcv2 rcv3 rcv_value rcv_name cv_conclusion
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <chr> <chr>
1 AAA 7 10 8 5 8 rcv2 ok
2 BBB 4 5 3 1 5 rcv1 ok
3 CCC 11 20 15 12 12 rcv3 ok
4 DDD 8 7 5 2 7 rcv1 ko
NB: my real data has more than 100 markers
and about 10 different rcv
.
Where I fail is getting the rcv_name
from the corresponding rcv_value
(using mutate
and case_when
).
Thanks for help.
Data:
dat0 <-
structure(list(marker = c("AAA", "BBB", "CCC", "DDD"), cv = c(7,
4, 11, 8), rcv1 = c(10, 5, 20, 7), rcv2 = c(8, 3, 15, 5), rcv3 = c(5,
1, 12, 2)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -4L))
min_
function from thehablar
package to circumvent that. Or write my own custom function. – Utter