Exercise

Summarzing Variables with rxCrossTabs()

We will use rxCrossTabs() to compute a few sums and category-specific means.

Instructions

100 XP

Use rxCrossTabs() to compute the total summed trading volume for each day of the week.

As we saw in an earlier exercise, the syntax of rxCrossTabs() is:

rxCrossTabs(formula, data, …)

  • formula - A formula containing the variables that you want to cross tabulate. This formula operates very similarly to the formula provided to the open-source function xtabs().
  • data - The dataset in which you want to look for variables specified in formula.
  • … - Additional arguments.

Previously, we used rxCrossTabs() to compute a simple frequency table. The last time we saw this function, we only specified a single variable on the right side of the formula. We can also specify a variable on the left side of the ~. If we specify such a response variable on the left side of the formula, rxCrossTabs() will treat it as a frequency count, and will sum that variable for each possible combination of variables on the right hand side. For example, we could use rxCrossTabs() in order to compute the total volume traded for each day of the week.

Use rxCrossTabs() to compute the total volume traded on each day of the week.

We can use rxCrossTabs() to compute sums according to combinations of different variables. For instance, if we wanted to compute the total volume traded for each day of the week for each month, we can specify the variables representing day of the week and the month on the right hand side of there formula, with a : separating them. In this case the Month variable is stored as an integer variable rather than a categorical variable, so we need to use the F() helper function to make sure that it is considered a categorical vector in the call to rxCrossTabs().

Use rxCrossTabs() to compute the total volume traded on each day of the week for each month. Remember that you can use rxGetVarInfo() to see the names of the variables in an xdf file, and remember to use F() on the month variable.

We can also use rxCrossTabs() to compute means rather than sums. If we specify a variable on the left side of the formula, and we set the means argumentTRUE, then rxCrossTabs() will return the means in each cell.

Now, use use rxCrossTabs() to compute the mean volume traded on each day of the week for each month.

Finally, we can also use frequency and probability weights via the fweights and pweights arguments, respectively.

Go ahead and use rxCrossTabs() to compute the mean closing price for each day of the week and month, using frequency weights associated with the volume traded.