More than two groups

The sitch

Here's the sitch from before. We have goat social media data. (Let's say the data is from a CSV file, though not much changes if it's in a Range.)

We want to compare coder and noncoder goats. So we make variables for each group:

  • sTotalTweetsCoders
  • sTotalFollowersCoders
  • sTotalAgeCoders
  • sNumberCanCode

And:

  • sTotalTweetsNoncoders
  • sTotalFollowersNoncoders
  • sTotalAgeNoncoders
  • sNumberCannotCode

We'd make variables for the averages as well, like...

  • sAverageTweetsCoders
  • sAverageTweetsNoncoders

... and so on.

Here's the processing code.

  • 'Loop across goats.
  • For sGoatIndex = 1 To sNumberValidRecords
  •     If atWriteCode(sGoatIndex) = "yes" Then
  •         'Accumulate data for coders.
  •         sNumberCanCode = sNumberCanCode + 1
  •         sTotalFollowersCoders = sTotalFollowersCoders + asTwitterFollowers(sGoatIndex)
  •         sTotalTweetsCoders = sTotalTweetsCoders + asTweets30Days(sGoatIndex)
  •         sTotalAgeCoders = sTotalAgeCoders + asAge(sGoatIndex)
  •     Else
  •         'Accumulate data for noncoders.
  •         sNumberCannotCode = sNumberCannotCode + 1
  •         sTotalFollowersNoncoders = sTotalFollowersNoncoders + asTwitterFollowers(sGoatIndex)
  •         sTotalTweetsNoncoders = sTotalTweetsNoncoders + asTweets30Days(sGoatIndex)
  •         sTotalAgeNoncoders = sTotalAgeNoncoders + asAge(sGoatIndex)
  •     End If
  • Next sGoatIndex

But now...

More than two groups

Ray
Ray

What if we wanted to compare three groups? Like goats who are young, goats who are middle-aged, and older goats?

Want to try that one, Adela?

Adela
Adela

No worries. Have total, count and average variables for each group.

  • sTotalTweetsYoung
  • sTotalFollowersYoung
  • sTotalAgeYoung
  • sNumberYoung

And:

  • sTotalTweetsMiddleAged
  • sTotalFollowersMiddleAged
  • sTotalAgeMiddleAged
  • sNumberMiddleAged

And:

  • sTotalTweetsOld
  • sTotalFollowersOld
  • sTotalAgeOld
  • sNumberOld

Make variables for the averages as well, like...

  • sAverageTweetsYoung
  • sAverageTweetsMiddleAged

... and so on.

Adela
Adela

Then, we change the processing code, to add to the variables for each group.

  • 'Loop across goats.
  • For sGoatIndex = 1 To sNumberValidRecords
  •     If asAge(sGoatIndex) < 35 Then
  •         'Accumulate data for young goats.
  •         sNumberYoung = sNumberYoung + 1
  •         sTotalFollowersYoung = sTotalFollowersYoung+ asTwitterFollowers(sGoatIndex)
  •         sTotalTweetsYoung = sTotalTweetsYoung + asTweets30Days(sGoatIndex)
  •         sTotalAgeYoung = sTotalAgeYoung + asAge(sGoatIndex)
  •     ElseIf asAge(sGoatIndex) < 55 Then
  •         'Accumulate data for middle-aged.
  •         sNumberMiddleAged = sNumberMiddleAged + 1
  •         sTotalFollowersMiddleAged = sTotalFollowersMiddleAged + asTwitterFollowers(sGoatIndex)
  •         sTotalTweetsMiddleAged = sTotalTweetsMiddleAged + asTweets30Days(sGoatIndex)
  •         sTotalAgeMiddleAged = sTotalAgeMiddleAged + asAge(sGoatIndex)
  •     Else
  •         'Accumulate data for old goats.
  •         sNumberOld = sNumberOld + 1
  •         sTotalFollowersOld = sTotalFollowersOld + asTwitterFollowers(sGoatIndex)
  •         sTotalTweetsOld = sTotalTweetsOld + asTweets30Days(sGoatIndex)
  •         sTotalAgeOld = sTotalAgeOld + asAge(sGoatIndex)
  •     End If
  • Next sGoatIndex
Marcus
Marcus

Is that the multiway if pattern?

Adela
Adela

Yes, Marcus, it is.

Pattern

Multiway

Use a chain of If statements.

A new pattern

Thanks, Adela. This give us a new pattern, for analyzing subsets.

Pattern

Analyzing subsets

Make accumulator variables for each subset, and an If for processing them.