繁体   English   中英

计数值在组中出现的次数 R

[英]Count number of times a value occurs within a group R

我的数据样本如下:

id = c(1, 2, 3, 4, 5, 1, 4, 7, 8, 3)
date = c("2020-12-31", "2020-12-31", "2020-12-31", "2020-12-31",
          "2020-12-31", "01-01-2021", "01-01-2021", "01-01-2021", "01-01-2021",
          "01-01-2021")
total = c(1, 4, 4, 15, 0, 12, 1, 1, 1, 0)
data = data.frame(id, date, total)

我试图计算每个日期出现“总”值的次数。 因此,例如,对于日期"2020-12-31" ,值4出现两次,但值1只出现一次,因为它在该日期出现150 然后对于日期"01-01-2021" ,值1出现 3 次,依此类推。 本质上,我希望 out 导致:

day = c("2020-12-31", "01-01-2021")
one = c(1, 3)
two = c(0, 0)
three = c(0, 0)
four = c(2, 0)
five = c( 0, 0)
six = c(0, 0)
seven = c(0,0)
eight = c(0, 0)
nine = c(0,0)
ten = c(0,0)
eleven = c(0,0)
twelve = c(0,1)
thirteen = c(0,0)
fourteen = c(0,0)
fifteen = c(1,0)
df = data.frame(day, one, two, three, four, five, six, seven, eight, nine, ten, eleven, twelve, thirteen,
                  fourteen, fifteen)

所以一列代表日期,接下来的 15 列代表我正在计算的数字。 (我的数据还有更多日期,我只是没有把它们都放在我的例子中)

我首先按以下方式对原始列进行分组:

data %>%
group_by(date, total)

但我不确定如何计算每组的值并将其放入生成的 dataframe 中。 谢谢!

library(tidyr)
library(dplyr)
data %>%
  count(date, total) %>%
  complete(date, total = 0:15, fill = list(n = 0)) %>%
  pivot_wider(id_cols = date, names_from = total, values_from = n, names_prefix = "total")
# # A tibble: 2 × 17
#   date   total0 total1 total2 total3 total4 total5 total6 total7 total8 total9 total10 total11 total12
#   <chr>   <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>   <dbl>   <dbl>   <dbl>
# 1 01-01…      1      3      0      0      0      0      0      0      0      0       0       0       1
# 2 2020-…      1      1      0      0      2      0      0      0      0      0       0       0       0
# # … with 3 more variables: total13 <dbl>, total14 <dbl>, total15 <dbl>

`as.data.frame.table 是历史悠久的方法:

as.data.frame( with(data, table(date, total)))
#------------------------
         date total Freq
1  01-01-2021     0    1
2  2020-12-31     0    1
3  01-01-2021     1    3
4  2020-12-31     1    1
5  01-01-2021     4    0
6  2020-12-31     4    2
7  01-01-2021    12    1
8  2020-12-31    12    0
9  01-01-2021    15    0
10 2020-12-31    15    1

如果您希望它采用“宽”格式,这确实是 ab*tch 可以使用,然后将其保留为 tble:

with(data, table(date, total))
            total
date         0 1 4 12 15
  01-01-2021 1 3 0  1  0
  2020-12-31 1 1 2  0  1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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