Amadeus cookies policy - you'll see this message only once.

Amadeus use cookies on this website. They help us to know a little bit about you and how you use our website, which improves the browsing experience and marketing - both for you and for others. They are stored locally on your computer or mobile device. To accept cookies, continue browsing as normal. Or, go to the privacy policy for more information.

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:

Subsetting Data Using the MOD Function Image 1

We want to split our data by Statistic, so we use the following code:

Subsetting Data Using the MOD Function Image 2

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.

Subsetting Data Using the MOD Function Image 3

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!