Subsetting Data Using the MOD Function
If we have data that follows a repeating pattern, it is possible to manipulate that data using only the observation number.
Suppose we have the following data set:
We want to split our data by Statistic, so we use the following code:
However, we want to save some space in our data set! If we use the MOD function, we don’t need the Statistic column.
The MOD function divides its first argument by its second argument and returns the remainder.
The pattern of different statistics repeats every 4 rows, so we want to subset the data using the different values returned by the MOD function applied to the observation number with the second argument being 4.
- MOD(1,4) translates to 1 divided by 4 which equals 0 with a remainder of 1.
- MOD(2,4) translates to 2 divided by 4 which equals 0 with a remainder of 2.
- MOD(7,4) translates to 7 divided by 4 which equals 1 with a remainder of 3.
- MOD(12,4) translates to 12 divided by 4 which equals 3 with a remainder of 0.
Looking back at the original code, we can replace the conditions applied to the output statements with conditions using the MOD function.
The code shown above outputs the same observations to each of the output tables as that it did in the old code, but does not require the Statistic column.
NOTE: If the pattern changes at any point, this method will produce unexpected output without producing an error or warning!