A fairly common statistical request is to understand the proportion of a set of observations which meet a particular condition. We’re going to show you a simple way to calculate proportion in r for vectors (and things that can be converted into vectors, such as specific fields within a dataframe).

To accomplish this, we need to combine two fundamental operations:

Calculate Proportion in R – Example

We’re going to use a business example here. Suppose we have hired a group of salespeople and are eagerly awaiting to see how they turn out. Maybe we put them through some kind of new training program.

For the sake of argument, let us state that any sales person who can sell $50,000 of product in their first couple of months is probably going to be a good hire. The proportion of the class below that mark is in grave danger of dropping out. A vector of values is presented below…

# Calculate proportion in R example

# define data for calculate proportion in R example
> salesresults <- c(45000,55000,95000,15000,23000,33000,112000,20000,30000,35000,65000)

# apply our statistical test to create a Boolean vector
> salesresults > 50000

# use the mean function to roll this up to a proportion
> mean (salesresults >50000)
[1] 0.3636364

# results of calculate proportion in r example

The math is straight forward. We define a Boolean test to convert our vector of values into a vector describing the outcome of a True / False proposition. The mean function values True at 1 and False at 0; we use this function to roll up the values into a single proportion.

Scroll to top
Privacy Policy