dataframe - Using conditionals and summary functions in R mutate -
i have data frame in r looks
v1 v2 v3 v4 animal 1 2 2 3 5 dog 2 2 4 3 1 dog 3 1 4 1 1 cat 4 5 5 1 3 cat 5 5 5 5 3 bird 6 3 3 3 4 bird
where used group_by group data animal. created new column v6 takes column v4, divides lower values higher values, , if value smaller .5 have v6= , ifelse has v6 = b.. there way using mutate function conditional statement in r? actual data frame larger rather not have manually. final data frame
v1 v2 v3 v4 animal v6 1 2 2 3 5 dog 2 2 4 3 1 dog 3 1 4 1 1 cat 4 5 5 1 3 cat 5 5 5 5 3 bird b 6 3 3 3 4 bird b
and have started
df %>% mutate(type = if(min/max < .5)a, ifelse, b)
but know not correct. thank you!
using dplyr
can try this
dat %>% group_by(animal) %>% mutate(new = ifelse(min(v4)/max(v4) < 0.5, "a", "b")) #source: local data frame [6 x 6] #groups: animal # x1 v2 v3 v4 animal new #1 2 2 3 5 dog #2 2 4 3 1 dog #3 1 4 1 1 cat #4 5 5 1 3 cat #5 5 5 5 3 bird b #6 3 3 3 4 bird b
Comments
Post a Comment