簡體   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