I would like to create columns based on the State, to count the total count for the year.
test <- data.frame(Year=c(rep(2005,3),rep(2006,3),rep(2007,3)), State = rep(c("AK","AZ","CA"),3), count=c(12312,43243,1234,6737,56,123,1,6,8))
test
Year State count
1 2005 AK 12312
2 2005 AZ 43243
3 2005 CA 1234
4 2006 AK 6737
5 2006 AZ 56
6 2006 CA 123
7 2007 AK 1
8 2007 AZ 6
9 2007 CA 8
Below is my desired output:
answer <- data.frame(State=c("AK","AZ","CA"), count2005=c(12312,43243,1234), count2006=c(6737,56,123), count2007=c(1,6,8))
answer
State count2005 count2006 count2007
1 AK 12312 6737 1
2 AZ 43243 56 6
3 CA 1234 123 8