I trying to filter values over columns accordingly to some conditions.
I have the following part of my data:
df <- tibble(
v1 = c(5, 7, 4, 2, 2, 3, 3, 4, 5, 6, 1, 7),
v2 = c(1, 1, 1, 5, 7, 30, 1, 4, 5, 6, 6, 7),
v3 = factor(c("a", "a", "a", "a", "a", "a", "b", "b", "b", "b", "b", "b"))
)
I trying to create a column test that verifies if each row by group (v3 in this case) are between the mean +- standard deviation (sd) of their group and return a logical value (T/F).
For example, if the first value in v1 (5) is between mean +- sd for group a in v1 and the first value in v2 (1) is between mean +- sd for group a in v2 the column test will return a TRUE in another case return FALSE.
The expected output I wait is:
# A tibble: 12 × 4
v1 v2 v3 test
<dbl> <dbl> <fct> <lgl>
1 5 1 a TRUE
2 7 1 a FALSE
3 4 1 a TRUE
4 2 5 a TRUE
5 2 7 a TRUE
6 3 30 a FALSE
7 3 1 b FALSE
8 4 4 b TRUE
9 5 5 b TRUE
10 6 6 b TRUE
11 1 6 b TRUE
12 7 7 b FALSE
I try the following code, but not return what I expected
df %>%
mutate(teste = if_all(v1:v2, ~ . <= mean(.) - sd(.) | . >= mean(.) + sd(.), TRUE, FALSE))
Thanks for any help
>Solution :
Try this:
df %>%
mutate(teste = if_all(v1:v2, ~ . >= mean(.) - sd(.) & . <= mean(.) + sd(.)),.by=v3)
v1 v2 v3 teste
<dbl> <dbl> <fct> <lgl>
1 5 1 a TRUE
2 7 1 a FALSE
3 4 1 a TRUE
4 2 5 a TRUE
5 2 7 a TRUE
6 3 30 a FALSE
7 3 1 b FALSE
8 4 4 b TRUE
9 5 5 b TRUE
10 6 6 b TRUE
11 1 6 b FALSE
12 7 7 b FALSE