mercredi 22 novembre 2017

How to define simultaneous if conditions

I have trouble with defining logic in basic if else loop.

In particular when defining simultaneous if conditions.

for example, if (all(x<0) and case when per<percent with (diff(x>0)).

Say we have a data frame,

df <- data.frame(
  gr=gl(3,5),
  percent = c(seq(0.1, 0.5,0.1), seq(-0.5,-0.1,0.1), seq(0.1, 0.5,0.1)),
  per=rep(c(0.25,-0.25,0.25),each=5),
  x=c(c(0,0,0,1,2), c(0,0.1,0,1,2),c(1,1,0,0.1,0)))

> df
   gr percent   per   x
1   1     0.1  0.25 0.0
2   1     0.2  0.25 0.0
3   1     0.3  0.25 0.0
4   1     0.4  0.25 1.0
5   1     0.5  0.25 2.0
6   2    -0.5 -0.25 0.0
7   2    -0.4 -0.25 0.1
8   2    -0.3 -0.25 0.0
9   2    -0.2 -0.25 1.0
10  2    -0.1 -0.25 2.0
11  3     0.1  0.25 1.0
12  3     0.2  0.25 1.0
13  3     0.3  0.25 0.0
14  3     0.4  0.25 0.1
15  3     0.5  0.25 0.0

I would like to create a new column named eastwood based on the logical check of data in per & percent columns.

Particularly, I have trouble with defining multiple conditions for dirty classification because some condition needs to satisfied at the same time for both columns.

to catch dirty, in gr 2 I define if conditional like this; if all percent<0 & (per<percent & diff(any(x>0))

to catch dirty, in gr 3 I define if conditional like this;

if(all(percent>0)&isTRUE(per0))))

enter image description here

data_manip <- function(x,per,percent){

      if(all(percent>0)&all(head(x,3)==0)&!isTRUE(per<percent&any(diff(x)>0))){
        "Good"
      }
      else

      #for gr 2    
      if(all(percent<0)&isTRUE(per>percent&any(diff(x)>0))){

        "Dirty"
      }
      else

      #for gr 3    
      if(all(percent<0)&isTRUE(per>percent&any(diff(x)>0))){
        "Dirty"
      }
    else
      NA  
    }


library(dplyr)
df %>%
  group_by(gr)%>%
  do(data.frame(.,eastwood=data_manip(.$x,.$per,.$percent)))




# A tibble: 15 x 5
# Groups:   gr [3]
       gr percent   per     x eastwood
   <fctr>   <dbl> <dbl> <dbl>   <fctr>
 1      1     0.1  0.25   0.0     Good
 2      1     0.2  0.25   0.0     Good
 3      1     0.3  0.25   0.0     Good
 4      1     0.4  0.25   1.0     Good
 5      1     0.5  0.25   2.0     Good
 6      2    -0.5 -0.25   0.0     <NA>
 7      2    -0.4 -0.25   0.1     <NA>
 8      2    -0.3 -0.25   0.0     <NA>
 9      2    -0.2 -0.25   1.0     <NA>
10      2    -0.1 -0.25   2.0     <NA>
11      3     0.1  0.25   1.0     <NA>
12      3     0.2  0.25   1.0     <NA>
13      3     0.3  0.25   0.0     <NA>
14      3     0.4  0.25   0.1     <NA>
15      3     0.5  0.25   0.0     <NA>

How can I apply multiple if statements with case_when in one line to catch dirty groups in data?

Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire