Skip to content

Instantly share code, notes, and snippets.

@watermouth
Created October 15, 2019 06:40
Show Gist options
  • Select an option

  • Save watermouth/69fdd9955bc028554750c437b8619ec6 to your computer and use it in GitHub Desktop.

Select an option

Save watermouth/69fdd9955bc028554750c437b8619ec6 to your computer and use it in GitHub Desktop.
取りうる値のうち存在しない値についてもカウントなどする
# x
# A tibble: 6 x 7
CNumber Year Month Side TekiyouCode Amount eDate
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <date>
1 XXX 2012 6 1 0 100000. 2012-06-30
2 XXX 2012 6 1 11 99999. 2012-06-30
3 XXX 2012 6 1 12 100001. 2012-06-30
4 XXX 2012 6 1 15 100001. 2012-06-30
5 XXX 2012 6 8 0 100002. 2012-06-30
6 XXX 2012 6 8 11 100001. 2012-06-30
library(dplyr)
library(tidyr)
x %>% select(-eDate) %>% mutate(TekiyouCode = factor(TekiyouCode, levels = 0:99), Side = factor(Side, levels = c(1,2,8,9))) %>%
complete(TekiyouCode, Side, nesting(CNumber, Year, Month), fill = list(Amount = 0))
# complete で埋める.
# factor変数について値が存在しない行を水増しできる
# 他の変数で組合せを考えるものは, nestingで指定する
# fill = list() で埋める値を設定する
@watermouth
Copy link
Author

group_by していた場合には, ungroupを忘れずに事前に実行しておく

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment