I have nested data of students in classes in schools.
If I have student number from 1... nth, classnumber from 1... nth and schoolnumber from 1... nth, how would I create a new column to count sequentially how many students are in each class. The new column would appear 1,2,3,4,5,1,2,3,4,1,2,3,4,5,6,1,2,1,2,3... nth. At each new classnumber the count begins again from 1.
At the minute I have gone a very long way round. I have table(classnr)
which gives me the number of pupils in each class. Then I have mydata$pupilinclass <- c(1:25, 1:7, 1:5, 1:15...
For a large dataset this is a lot of lines.
There must be a quicker way of doing this - can anyone help?