I'd use the length
function as a very simple option
100*(length(df$coins[df$coins > 20]) /length(df$coins))
100*(length(df$coins[df$coins == 20])/length(df$coins))
100*(length(df$coins[df$coins < 20]) /length(df$coins))
Giving
> 100*(length(df$coins[df$coins > 20]) /length(df$coins))
[1] 20
> 100*(length(df$coins[df$coins == 20])/length(df$coins))
[1] 50
> 100*(length(df$coins[df$coins < 20]) /length(df$coins))
[1] 30
If you are doing this a lot you could wrap it into a function, which you could use for other columns (d
) and/or values of interest (p
)
perc <- function(d, p){
return(c(
100*(length(d[d>p]) /length(d)),
100*(length(d[d==p])/length(d)),
100*(length(d[d<p]) /length(d))))
}
perc(df$coins, 20)
perc(df$coins, 90)
perc(df$copay, 10)
This is based off a reproducible data frame of
df <- data.frame("plan" = LETTERS[1:10], "coins" = c(20,15,30,30,5,20,20,20,10,20), "copay" = c(10,5,0,10,10,0,0,10,3,7))
Side note: Given the variety of answers you got, I was curious enough to compare the approaches used. I thought that was really great to see different peoples creative approaches!
Running 10,000 times on the provided data frame, there are some considerable differences in run speed (using code presented at the time of writing). Akrun and Hpesoj626's solutions took 37 and 40 seconds respectively, Gregor's was considerably quicker at 2.1 seconds, while mine ran in 0.61 seconds. Furthermore, if you wrap it in to a function as I suggested, it takes just 0.15 seconds for 10,000 runs.
Gregor's uses fewer characters thus is a shorter script, personally I think it's very elegant (though if you are doing this many times for different values or columns the function will be the shortest approach). My only concern would be how it handles continuous data - imagine coins could take the value 20.0000000000001 - you would then have to code it as something like ...-Inf, 19.99999999999, 20.0000000000001, Inf
... In other words, you have to be very careful about how you implement it.
As Gregor has noted, my version would require some more modification if you were looking to have more intervals.