2
votes

I currently have a data frame in R that looks like this

   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 I used group_by to group the data by animal. I would like to created a new column V6 that takes column V4, divides the lower values by the higher values, and if that value is smaller than .5 have V6= A and ifelse has V6 = b.. Is there a way do to that using the mutate function with conditional statement in R? The actual data frame is much larger so I would rather not have to do it manually. This is what I would like the final data frame to look like

   V1 V2 V3 V4 Animal V6

1  2  2  3  5  Dog  A

2  2  4  3  1  Dog  A

3  1  4  1  1  Cat  A

4  5  5  1  3  Cat  A

5  5  5  5  3  Bird B

6  3  3  3  4  Bird B

And this is what I have started with

df %>% mutate(Type = if(min/max < .5)A,
              ifelse, B)

But I know this is not correct. Thank you!

2
If you take only column V4 where min = 1 and max = 5 you obtain for all values of V6 = 1/5 which is 0.2 and so always A. Please explain well. Maybe you meant from V1 to V4 - SabDeM
The min and max value part is not clear - akrun
I would like to do it by animal, so that the min value of V4 is divided by the max value of V4 for each individual animal, and then V6 has different values for each animal (A or B) - user4999605
dat %>% group_by(Animal) %>% mutate(new = ifelse(min(V4)/max(V4) < 0.5, "A", "B")) - Veerendra Gadekar
I didn't notice the group by as in your original code there was no group - akrun

2 Answers

3
votes

Using dplyr you 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   A
#2  2  4  3  1    Dog   A
#3  1  4  1  1    Cat   A
#4  5  5  1  3    Cat   A
#5  5  5  5  3   Bird   B
#6  3  3  3  4   Bird   B
1
votes

You could also do this without ifelse

 df1 %>% 
   group_by(Animal) %>% 
   mutate(Type= c('A', 'B')[((min(V4)/max(V4)) >=0.5)+1L])
#  V1 V2 V3 V4 Animal Type
#1  2  2  3  5    Dog    A
#2  2  4  3  1    Dog    A
#3  1  4  1  1    Cat    A
#4  5  5  1  3    Cat    A
#5  5  5  5  3   Bird    B
#6  3  3  3  4   Bird    B