mercredi 1 mai 2019

how to apply multiple "if" functions by group in R

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 expext 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!

Aucun commentaire:

Enregistrer un commentaire