Alexander
Alexander

Reputation: 4635

if statement in grouped data

I am trying to add new column named 'class' to my data based on the conditions of the columns. I built user defined function called class_fun to create this new column.

I try to set entire sub-group to same value if the condition is matched. But I have not succeeded yet!

test=data.frame(set=as.numeric(gl(3,6)),gr = rep(letters[1:2],each=3),vals=c(c(10,10, 9, 8, 1,1),c(10,10,10, 9,6,2),c(10,7,6,1,1,2)))

> test
   set gr vals
1    1  a   10
2    1  a   10
3    1  a    9
4    1  b    8
5    1  b    1
6    1  b    1
7    2  a   10
8    2  a   10
9    2  a   10
10   2  b    9
11   2  b    6
12   2  b    2
13   3  a   10
14   3  a    7
15   3  a    6
16   3  b    1
17   3  b    1
18   3  b    2

here is the function for creating a new column named class.

class_fun <- function(gr,set,vals){
  if(any(grepl("a|b",gr)&set==vals)){

    "catched"

  }
  else{

    NA
  }
}

library(dplyr)
test%>%
  group_by(set,gr)%>%
  mutate(class=class_fun(gr,set,vals))

which gives

# A tibble: 18 x 4
# Groups:   set, gr [6]
     set gr     vals class
   <dbl> <fct> <dbl> <lgl>
 1     1 a        10 NA   
 2     1 a        10 NA   
 3     1 a         9 NA   
 4     1 b         8 NA   
 5     1 b         1 NA   
 6     1 b         1 NA   
 7     2 a        10 NA   
 8     2 a        10 NA   
 9     2 a        10 NA   
10     2 b         9 NA   
11     2 b         6 NA   
12     2 b         2 NA   
13     3 a        10 NA   
14     3 a         7 NA   
15     3 a         6 NA   
16     3 b         1 NA   
17     3 b         1 NA   
18     3 b         2 NA 

What I was expecting is

# A tibble: 18 x 4
# Groups:   set, gr [6]
     set gr     vals class
   <dbl> <fct> <dbl> <lgl>
 1     1 a        10 NA   
 2     1 a        10 NA   
 3     1 a         9 NA   
 4     1 b         8 catched   
 5     1 b         1 catched
 6     1 b         1 catched
 7     2 a        10 NA   
 8     2 a        10 NA   
 9     2 a        10 NA   
10     2 b         9 catched   
11     2 b         6 catched   
12     2 b         2 catched  
13     3 a        10 NA   
14     3 a         7 NA   
15     3 a         6 NA   
16     3 b         1 NA   
17     3 b         1 NA   
18     3 b         2 NA   

any idea or solution why this is happening ? thx!

Upvotes: 0

Views: 190

Answers (1)

BENY
BENY

Reputation: 323226

I think no need to write a function .

test%>%group_by(set,gr)%>%mutate(calss=ifelse(any(stringr::str_detect(gr,'a|b')&(set==vals)),'catched',NA)
+ )
# A tibble: 18 x 4
# Groups:   set, gr [6]
     set     gr  vals   calss
   <dbl> <fctr> <dbl>   <chr>
 1     1      a    10    <NA>
 2     1      a    10    <NA>
 3     1      a     9    <NA>
 4     1      b     8 catched
 5     1      b     1 catched
 6     1      b     1 catched
 7     2      a    10    <NA>
 8     2      a    10    <NA>
 9     2      a    10    <NA>
10     2      b     9 catched
11     2      b     6 catched
12     2      b     2 catched
13     3      a    10    <NA>
14     3      a     7    <NA>
15     3      a     6    <NA>
16     3      b     1    <NA>
17     3      b     1    <NA>
18     3      b     2    <NA>

Upvotes: 1

Related Questions