Tech Tips: Random Variables Described by Tables

Often one is given (or can compute) a table that represents the probability mass function for a given discrete random variable of interest. One can use both R and Excel, in combination with such a table, to find expected values, variances, and standard deviations for the related discrete random variable. One can also use R and Excel to simulate any number of realizations of that random variable.

The following demonstrates these things for a random variable $X$ whose probability mass function is given by: $$\begin{array}{l|c|c|c|c} X & -4 & 2 & 5 & 10\\\hline P(X) & 0.50 & 0.30 & 0.15 & 0.05 \end{array}$$


Calculating $E(X)$, $Var(X)$, and $SD(X)$ for a Random Variable Described by a Table

Simulating Realizations of a Random Variable Described by a Table

Often, one wishes to simulate some number of realizations of a discrete random variable whose probability mass function given as a table. Again using the random variable $X$ with probability mass function

$$\begin{array}{l|c|c|c|c} X & -4 & 2 & 5 & 10\\\hline P(X) & 0.50 & 0.30 & 0.15 & 0.05 \end{array}$$

We can accomplish these simulations with: