mardi 17 avril 2018

applying simple function to data

Hi I try to apply simple function to data to create sub_id for groups.

test = data.frame(gr=gl(2,4), id =rep(c("Good","bad","ugly","dirty"),2),
                        count=c(175,1,13,11, 10,165,10,2))  


> test
  gr    id count
1  1  Good   175
2  1   bad     1
3  1  ugly    13
4  1 dirty    11
5  2  Good    10
6  2   bad   165
7  2  ugly    10
8  2 dirty     2

the condition for sub_id is like this

if group number is equal to minimum count with when id==Bad it is red flag else green flag. That's it!

So wrote this function

  sub_id <- function(gr,count,id){
    if (gr==min(count)&id=="Bad"){
      "red flag"

    }
    else
    "green flag"  
  }

and tried

library(dplyr)

  test%>%
    group_by(gr)%>%
    mutate(color=sub_id(gr,count,id))

gives me

# A tibble: 8 x 4
# Groups:   gr [2]
      gr     id count      color
  <fctr> <fctr> <dbl>      <chr>
1      1   Good   175 green flag
2      1    bad     1 green flag
3      1   ugly    13 green flag
4      1  dirty    11 green flag
5      2   Good    10 green flag
6      2    bad   165 green flag
7      2   ugly    10 green flag
8      2  dirty     2 green flag
Warning messages:
1: In if (gr == min(count) & id == "Bad") { :
  the condition has length > 1 and only the first element will be used
2: In if (gr == min(count) & id == "Bad") { :
  the condition has length > 1 and only the first element will be used

the expected output

      gr     id count      color
  <fctr> <fctr> <dbl>      <chr>
1      1   Good   175   red flag
2      1    bad     1   red flag
3      1   ugly    13   red flag
4      1  dirty    11   red flag
5      2   Good    10 green flag
6      2    bad   165 green flag
7      2   ugly    10 green flag
8      2  dirty     2 green flag

Aucun commentaire:

Enregistrer un commentaire