首页 > 解决方案 > R中的字符变量分组

问题描述

我想制作一个数据框,显示按一个变量分组的字符变量的发生率,在本例中为 c2_inicio。我有如下数据:

     c2_inicio usuario_inicia          f_alarm
1  C2 Poniente       PH903731            DELITO
2     C2 Norte       C4895756          SERVICIO
3   C2 Oriente        Sistema      FALSA ALARMA
4  C2 Poniente        Sistema      FALSA ALARMA
5     C2 Norte       C4895646          SERVICIO
6    C2 Centro       C4895678 URGENCIAS MEDICAS
7   C2 Oriente       C5PEPK86        EMERGENCIA
8  C2 Poniente       C5ROGK95      FALTA CÍVICA
9  C2 Poniente        Sistema      FALSA ALARMA
10 C2 Poniente        Sistema      FALSA ALARMA

我需要这样的表:

  c2_inicio   incidence_user    incidence_DELITO   incidence_EMERGENCY
1 C2 Centro        38582       [number of DELITOS] [number of EMERGENCIES]
2 C2 Norte         76632       [...by c2_inicio]
3 C2 Oriente       77075
4 C2 Poniente      72333
5 C2 Sur           59137
6 C4i                 65

我试过了:

base_c2 <- c4%>%group_by(c2_inicio)%>%summarise(incidentes=n())

但只计算每个 c2_inicio 的观察总数。

我怎样才能做到这一点?先进的Thx ;)

标签: r

解决方案


推荐阅读