- 追加された行はこのように表示されます。
- 削除された行は
このように表示されます。
! cumsum()で累積和ベクトルを求める
監査対象となるとあるデータの母集団全体を眺めているときに、たとえば、すべての売上データを見ている際に、個々の売上取引がどのように分布しているかを見ることがあるが、それとは別の視点で、どの程度の件数が売上高全体のどの程度をカバーしているかを見たくなる。
パレート図のようなものだが、それを作ると、件数でどの程度カバーすれば、金額でどの程度カバーできるかという情報が、グラフによって感覚的に得られる。
> x <- c(1:10)
> x
[1] 1 2 3 4 5 6 7 8 9 10
> cumsum(x)
[1] 1 3 6 10 15 21 28 36 45 55
> x <- c(10:1)
> x
[1] 10 9 8 7 6 5 4 3 2 1
> cumsum(x)
[1] 10 19 27 34 40 45 49 52 54 55
> plot(cumsum(x))
:注意: summarise(cumN = cumsum( n()) )では処理してくれないし、エラーも出ない
df %>% group_by(...) %>% summarise(N = n()) %>% mutate(cumN = cumsum(N)) #OK
df %>% group_by(...) %>% summarise(N = n(), cumN = cumsum(n() ) #NG
!二系統(単独と累計)のグラフを複数描画する
> data = tibble(Week = 1L:12L, apple = runif(12, 5L, 20L), banana = runif(12, 8L, 35L))
> data %>%
pivot_longer(cols = c("apple", "banana")) %>%
group_by(name) %>% group_by(name) %>%
mutate(cum = cumsum(value)) %>%
mutate(cum = cumsum(value)) %>% #cumsumはgroup_byに応じて項目別に計算される
ggplot() +
geom_bar(aes(x = Week, y = value, fill = name), stat = "identity", position = position_dodge(.7)) + #position="dodge"でもよい
geom_line(aes(x = Week, y = cum, color = name))
!参照
経験累積分布関数(ECDF)