"And it came to pass after the plague" (Numbers 25:19). A parable: this is like a shepherd into whose flock wolves entered, and he was counting his flock to know how many remained. So too, He did this out of His love for Israel, to know and to make known how beloved they are before Him. And this counting was in order that the Land would be apportioned according to the names of these fathers' houses; for as there were fathers' houses within a tribe, so the land that came to that tribe was divided according to the number of the fathers' houses.
Why God Counted Israel After the Plague at Peor
Midrash Aggadah, Numbers 25:19
ויהי אחרי המגפה. משל לרועה שנכנסו זאבים לעדרו, והיה מונה עדרו במה נשארו, ולחיבתן של ישראל עשה, לידע ולהודיע כמה הם חביבין לפניו, ומנין זה היה כדי שעל שמם של אלו בתי אבות תחלק הארץ, לפי שהיה בתי אבות בשבט, כך הארץ המגיע לשבט וחולקת למספר בתי האבות: