I want to write a loop that can aggregate the number of instances (of certain values) that are grouped by year. More specifically, say the variable is x1
. I want to have two groups, one is when x1
= 1, and the other when it is a combination of some values (2,3, and 5 in the below example):
year x1
2000 1
2000 1
2000 2
2000 3
2000 5
The end result should look like this:
year x2 x3
2000 2 3
where x2
and x3
are the counts when x1
= 1 and x1
= c(2,3,5), respectively. How can one accomplish this?
Edit: Probably should have mentioned this earlier. I work with two datasets; one df1
is yearly (spanning approx. 200 years) and the other df2
is incident-based (around 50k observations; this is where x1
is currently located). So the idea of the loop is to look at each year[i] in df2
and aggregate the counts by grouping them as x2
and x3
in df1
.
Edit2: Ah, I solved why the submitted answers were not working for me. Apparently I ran into the dplyr
before plyr
problem discussed in this answer; I followed ManneR's answer and detached plyr. Now the group_by
command works again.