-1

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
....
pnuts
  • 58,317
  • 11
  • 87
  • 139
AlketCecaj
  • 117
  • 1
  • 11
  • Try `with(data, ave(seq_along(area), area, FUN=seq_along))` or `library(splitstackshape); getanID(data, 'area')` – akrun Jun 23 '15 at 14:28
  • Probably a dupe of https://stackoverflow.com/questions/18487109/create-counter-with-multiple-variables – talat Jun 23 '15 at 14:36
  • Not exactly. Im not looking for progressive counter as in the example indicated but for a conter of the intersection between kind and area. I need to count per area and per kind. – AlketCecaj Jun 23 '15 at 14:55

1 Answers1

2

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
phiver
  • 23,048
  • 14
  • 44
  • 56