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?