Created
October 15, 2019 06:40
-
-
Save watermouth/69fdd9955bc028554750c437b8619ec6 to your computer and use it in GitHub Desktop.
取りうる値のうち存在しない値についてもカウントなどする
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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() で埋める値を設定する |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
group_by していた場合には, ungroupを忘れずに事前に実行しておく