jeudi 25 juin 2015

conditional elseif statements in R

I have a data framethat looks like this

Animal
species sum
A       2
A       6
B       8
B       1
C       6
C       3
D       5
D       4

I wants the code that divides the min sum by the max sum for each species and creates a new column called ratio, and if that number is greater than .2 I want it to return that ratio, but if it is smaller than .2, I want it to return NA. I would like to do this in dplyr This is the code I have currently but it is not working I have this string of commands in R

animal <- animal %>% 
+     group_by(species) %>% 
+     mutate(ratio = ifelse((min(sum)/max(sum) > .2), (min(sum)/max(sum)), "NA"))

Thanks!

This is what it should look like in the end

species sum   ratio
A       2   .333
A       6   .333
B       8    NA
B       1    NA
C       6    .5
C       3    .5
D       5    .8
D       4    .8

Aucun commentaire:

Enregistrer un commentaire