Ho to group by in R by counting rows of a certain type?

75 views Asked by At

I have the following data

 subject_id;sign;long;lat;kind;area\n
361869449;;7.66839336;45.03530199;U;Torino\n
361890869;;7.63652742;45.0744409;S;Torino\n 361890954;;7.64516603;45.05570822;S;Torino\n 
361870497;;9.18559126;45.45859693;S;Milano\n 361870607;;12.49623211;41.9212079;S;Roma 361872151;;9.46181677;45.54500293;U;Milano 361875393;;12.42673437;41.87965563;S;Roma

How can I group by area and by city in order to have the results like this for example :

area, kind, counter
Torino, U, 1
Torino, S, 2
Roma, S, 2
Milano, S, 1
....
1

There are 1 answers

0
phiver On BEST ANSWER

I put your data in a textfile and loaded it into a data.frame df. Then the following will work.

library(dplyr)
tally(group_by(df, area, kind))

  area kind n
1 Milano    S 1
2 Milano    U 1
3   Roma    S 2
4 Torino    S 2
5 Torino    U 1