count - IN R counting hierarchical data -


I have a list of counties in each state that received non-prevention status from 1995 to 2005.

I want to know how many counties in each state get this status every year.

If my data is formatted like this then

  State1 country 1 YR1 Yr2 Yr3 Yr4 .. State 1 country 2 YR1 Yr2 Yr3 Yr4 State2 County 1 Yr1 Yr2 .. ...  

The variables of each year can be 1 or 1 zero, because a county can get or lose this status at a time.

I need to count every year that how many counties in each state have non-empathy (YRX = 1), but it is not possible to think about how to do it.

I used the following example:

  data & lt ; 1 State 1 County 6 0 0 0 State 2 County 7 0 0 0 State E2 County 8 1 0 0 State 1 County 9 0 0 State 2 County 10 0 1 State 3 County 11 1 1 1 State 3 County 12 0 0 0 State 3 County 13 0 1 1 State 3 County 14 0 0 0 1 State 4 County 15 0 0 State 4 County 16 1 0 1 0 State 4 County 17 0 0 State 4 County 18 1 1 1 1 ") , Header = t library (new Car 2) Data 2 - melt (data, id = c ("state", "county")) cast (data 2, state ~ variable, fun = yoga)  

Results:

  State Yr1 Yr2 Yr3 Yr4 1 State 1 1 2 2 2 State 2 1 1 1 1 3 State 3 1 2 2 2 4 State 4 2 1 2 1  

Comments