Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

create logical column based on multiple existing columns R

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).

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading