I have an R datframe like so:
Reason ID
Bulls eye A
Rsh AVG A
Bulls eye A
Rsh AVG A
Rsh AVG A
Rsh AVG B
Rsh AVG B
Rsh AVG B
Haze - front A
Rsh AVG A
Rsh AVG A
Rsh AVG A
Rsh SD B
Rsh SD A
Particle A
Bulls eye A
Rsh AVG A
Rsh AVG A
Rsh AVG A
Rsh AVG A
Rsh AVG A
System A
System A
System A
Rsh SD A
Particle C
Particle C
Particle A
Bulls eye A
Scratch - front A
Bulls eye A
RPM C
RPM C
I want to find the count of each type of reason grouped by the ID So my output table should look something like this:
ID Bulls eye Rsh AVG Haze Rsh SD Particle System Scratch RPM
A 5 11 1 2 2 3 1 0
B 0 3 0 1 0 0 0 0
C 0 0 0 0 2 0 0 2
I tried creating a vector with unique names from the Reason column and used the aggregate function in a for loop like so:
unq_reason<-unique(rframe$Reason)
df1<- for (i in unq_L1){
aggregate(Reason~ID, rframe, function(x) sum(x==i))
}
But that only works for counting one variable. How do i get the columns?