简体   繁体   中英

Count with condition

I would like to count how often one variable id_tasks occurs per month. The month variable is from 1 to 12.

So far i have only managed to count how often, each task occurs with the help of: I would like to know how often the task occurs in every month as the output, in order to detect which month has the most/least tasks.

count(df,c('id_task'))
id_task id_user day completion_yesno day_created has_deadline deadline created_before active overdue completed_before month 
16416   37033    5272  61                0          61            1      172              0      0       0                0
16417   37033    5272  62                0          61            1      172              2      2       0                0
16418   37033    5272  63                0          61            1      172              2      2       0                0
16419   37033    5272  64                0          61            1      172              2      2       0                0
16420   37033    5272  65                0          61            1      172              2      2       0                0
16421   37033    5272  66                0          61            1      172              2      2       0                0
16422   37033    5272  67                0          61            1      172              2      2       0                0
16423   37033    5272  68                0          61            1      172              2      2       0                0
16424   37033    5272  69                0          61            1      172              2      2       0                0
16425   37033    5272  70                0          61            1      172              2      2       0                0
16426   37033    5272  71                0          61            1      172              2      2       0                0
16427   37033    5272  72                0          61            1      172              2      2       0                0
16428   37033    5272  73                0          61            1      172              2      2       0                0
16429   37033    5272  74                0          61            1      172              2      2       0                0
16430   37033    5272  75                0          61            1      172              2      2       0                0
16431   37033    5272  76                0          61            1      172              2      2       0                0
16432   37033    5272  77                0          61            1      172              2      2       0                0
16433   37033    5272  78                0          61            1      172              2      2       0                0
16434   37033    5272  79                0          61            1      172              2      2       0                0
16435   37033    5272  80                0          61            1      172              2      2       0                0

desired output:

id_task  month freq
1         12    3
2          1    20

if you want to count the occuriencies of all the month X task combinations, table is your function:

table(df[, c("month", "id_task")])

You can rerun this with this dummy data:

df <- data.frame(id_task= sample.int(15, 100, replace = TRUE), month = rep(1:12, length.out=100))
table(df[, c("month", "id_task")])

If you want the sum of tasks per month just drop the task column and run it like this:

table(df[, c("month")])

You can ask with the dplyr package the following:

data %>%
> group_by(month) %>%
> count(id_task)

I think this will do. (:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM