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
3 个答案:
答案 0 :(得分:3)
I would avoid ifelse because both you will have to evaluate the whole vector anyway, and because you need to calculate the whole process twice. Here's how I would do this
library(data.table)
setDT(Animal)[, ratio := {r <- range(sum); r[1L]/r[2L]}, by = species]
Animal[ratio <= .2, ratio := NA]
# species sum ratio
# 1: A 2 0.3333333
# 2: A 6 0.3333333
# 3: B 8 NA
# 4: B 1 NA
# 5: C 6 0.5000000
# 6: C 3 0.5000000
# 7: D 5 0.8000000
# 8: D 4 0.8000000
答案 1 :(得分:1)
You can use NA_real_ to make the types compatible
animal %>%
group_by(species) %>%
mutate(ratio= ifelse((min(sum)/max(sum))> .2,
round((min(sum)/max(sum)),2), NA_real_))
# species sum ratio
#1 A 2 0.33
#2 A 6 0.33
#3 B 8 NA
#4 B 1 NA
#5 C 6 0.50
#6 C 3 0.50
#7 D 5 0.80
#8 D 4 0.80
A base R option would be
animal$ratio <- with(animal, ave(sum, species, FUN=function(x) {
x1 <- min(x)/max(x)
NA^(x1 <= 0.2)*x1 }))
答案 2 :(得分:1)
animal <- animal %>%
group_by(species) %>%
mutate(ratio = ifelse((min(sum)/max(sum) > .2), (min(sum)/max(sum)), as.numeric(NA)))
> animal
Source: local data frame [8 x 3]
Groups: species
species sum ratio
1 A 2 0.3333333
2 A 6 0.3333333
3 B 8 NA
4 B 1 NA
5 C 6 0.5000000
6 C 3 0.5000000
7 D 5 0.8000000
8 D 4 0.8000000