Table of Contents
Expected value, also known as mean or average, is a statistical concept that represents the long-term average outcome of a random variable. It is a useful measure for making predictions and decisions in various fields such as finance, economics, and probability.
In R, the expected value can be calculated by using the “mean” function. This function takes a vector of numerical values as its input and returns the average of those values as the output. For example, if we have a vector of numbers representing the outcomes of rolling a fair six-sided die (1, 2, 3, 4, 5, and 6), we can use the mean function to calculate the expected value as follows:
> outcomes <- c(1, 2, 3, 4, 5, 6) # create a vector of outcomes
> mean(outcomes) # calculate the expected value
[1] 3.5
This means that the expected value of rolling a fair six-sided die is 3.5. This value can be interpreted as the long-term average of all the possible outcomes of rolling the die.
Another example of calculating expected value in R is in the field of finance. Let’s say we have a portfolio of stocks with different expected returns and probabilities. We can use the mean function to calculate the expected value of the portfolio’s overall return, which can help us make informed investment decisions.
In conclusion, the expected value can be easily calculated in R using the “mean” function. It is a useful tool for making predictions and decisions in various fields and can provide valuable insights into the long-term outcomes of random variables.
Calculate Expected Value in R (With Examples)
A probability distribution tells us the probability that a takes on certain values.
For example, the following probability distribution tells us the probability that a certain soccer team scores a certain number of goals in a given game:

To find the expected value of a probability distribution, we can use the following formula:
μ = Σx * P(x)
where:
- x: Data value
- P(x): Probability of value
For example, the expected number of goals for the soccer team would be calculated as:
μ = 0*0.18 + 1*0.34 + 2*0.35 + 3*0.11 + 4*0.02 = 1.45 goals.
To calculate expected value of a probability distribution in R, we can use one of the following three methods:
#method 1 sum(vals*probs) #method 2 weighted.mean(vals, probs) #method 3 c(vals %*% probs)
All three methods will return the same result.
The following examples show how to use each of these methods in R.
Example 1: Expected Value Using sum()
The following code shows how to calculate the expected value of a probability distribution using the sum() function:
#define values
vals <- c(0, 1, 2, 3, 4)
#define probabilities
probs <- c(.18, .34, .35, .11, .02)
#calculate expected value
sum(vals*probs)
[1] 1.45
Example 2: Expected Value Using weighted.mean()
#define values vals <- c(0, 1, 2, 3, 4) #define probabilities probs <- c(.18, .34, .35, .11, .02) #calculate expected value weighted.mean(vals, probs) [1] 1.45
Example 3: Expected Value Using c()
The following code shows how to calculate the expected value of a probability distribution using the built-in c() function in R:
#define values vals <- c(0, 1, 2, 3, 4) #define probabilities probs <- c(.18, .34, .35, .11, .02) #calculate expected value c(vals %*% probs) [1] 1.45
Notice that all three methods returned the same expected value.
Cite this article
stats writer (2024). How can the expected value be calculated in R with examples?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-the-expected-value-be-calculated-in-r-with-examples/
stats writer. "How can the expected value be calculated in R with examples?." PSYCHOLOGICAL SCALES, 4 May. 2024, https://scales.arabpsychology.com/stats/how-can-the-expected-value-be-calculated-in-r-with-examples/.
stats writer. "How can the expected value be calculated in R with examples?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-the-expected-value-be-calculated-in-r-with-examples/.
stats writer (2024) 'How can the expected value be calculated in R with examples?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-the-expected-value-be-calculated-in-r-with-examples/.
[1] stats writer, "How can the expected value be calculated in R with examples?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.
stats writer. How can the expected value be calculated in R with examples?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
