-1

Here is an example of the panel dataset I'm working with:

library(data.table)
data <- data.table(ID = c(1,1,1,1,1,2,2,2,2),
                   crop = c(1,2,3,4,5,1,2,3,4))

ID, crop
1, 1
1, 2
1, 3
1, 4
1, 5
2, 1
2, 2
2, 3
2, 4

There are several ID variables each with a varying number of observations (rows) according to the number of crop's they have.

I want to create an additional variable that shows the total number of observations an ID has. The desired output would look like:

ID, crop, total
1, 1, 5
1, 2, 5
1, 3, 5
1, 4, 5
1, 5, 5
2, 1, 4
2, 2, 4
2, 3, 4
2, 4, 4

Is this possible to do using data.table in R?

codemachino
  • 103
  • 9

1 Answers1

3

You could use

library(data.table)

data[, total := .N, by = ID]

This returns

   ID crop total
1:  1    1     5
2:  1    2     5
3:  1    3     5
4:  1    4     5
5:  1    5     5
6:  2    1     4
7:  2    2     4
8:  2    3     4
9:  2    4     4
Martin Gal
  • 16,640
  • 5
  • 21
  • 39