0

I'm trying to apply some basic "if" by group over a large dataset in R.

I tried to write a function and to apply over the groups using dplyr but it's not working. What could be the problem?

#dataframe
db <- data.frame(ID=c(1,1),
                 type=c("a","b"),
                 qual=c("no","OK"))
#if (no problem)
attach(db)
if(db[type  =="a","qual"]=="OK"){
  db[type =="a","qual_fin"] <- "OK"
  db[type =="b","qual_fin"] <- "no"
} else if ( db[type  =="b","qual"]=="OK"){
  db[type  =="b","qual_fin"] <- "OK"
  db[type  =="a","qual_fin"] <- "no"
} else {db$qual_fin <- "no"
} 

#dataframe with groups
db <- data.frame(ID=c(1,1,2,2),
                 type=c("a","b","a","b"),
                 qual=c("OK","OK","no","OK"))

#function
quality <- function( a,b, qual_fin_a,qual_fin_b){
  if(a =="OK"){
    qual_fin_a <- "OK"
    qual_fin_b <- "no"
  } else if ( b =="OK"){
    qual_fin_b <- "OK"
    qual_fin_a <- "no"
  } else {qual_fin_a <- "no"
  qual_fin_b <- "no"
  }}

#if by group
library(dplyr)

db2 <- db %>%   
  group_by(ID) %>% 
  do(quality(a=db[db$type  =="a","qual"],
             b=db[db$type  =="b","qual"],
             qual_fin_a=db[db$type=="a","qual_fin"],
             qual_fin_b=db[db$type=="b","qual_fin"]))

I expect this result:

> db
  ID type qual qual_fin
1  1    a   OK       OK
2  1    b   OK       no
3  2    a   no       no
4  2    b   OK       OK

I imagine that the solution is pretty simple but I'm struggling to find it!

Parfait
  • 104,375
  • 17
  • 94
  • 125
Matt_4
  • 147
  • 1
  • 12
  • 5
    Use `ifelse` instead of `if/else`. or use `case_when`. – akrun May 01 '19 at 15:34
  • 2
    Or check out `case_when` from dplyr. You need to make a statement that when fed a vector, will return a vector. `if/else` is a control statement, it's not a vectorized statement. – MrFlick May 01 '19 at 15:37
  • Possible duplicate of [Can dplyr package be used for conditional mutating?](https://stackoverflow.com/questions/24459752/can-dplyr-package-be-used-for-conditional-mutating) – divibisan May 01 '19 at 17:07

0 Answers0