r-将日期与pivot_wider一起拆分



我有下表。

类别
日期
1999年2月15日 A
1999年2月15日 A
1999年2月15日 B
1999年5月15日 A
1999年5月15日 B
1999年10月15日 C
1999年10月15日 C
2001年2月15日 A
2001年2月15日 A
2001年6月15日 B
2001年6月15日 B
2001年6月15日 C
2001年11月15日 C
2001年11月15日 C

您可以使用tidyverse包来完成此操作。首先,将日期列格式化为日期,然后按月计数,转到更宽的范围并格式化表格。

library(tidyverse)
data %>% 
mutate(Date = as.Date(Date, format = "%d/%m/%Y")) %>% 
group_by(Cat, month = lubridate::floor_date(Date, "month")) %>% 
count(Cat) %>% 
pivot_wider(names_from = Cat, values_from = n, values_fill = 0) %>% 
mutate(year = year(month), .before = "A",
month = month(month, label = T, abbr = F)) %>% 
mutate(Total = rowSums(across(A:C))) %>% 
arrange(year)
month     year     A     B     C Total
<ord>    <dbl> <int> <int> <int> <dbl>
1 February  1999     2     1     0     3
2 May       1999     1     1     0     2
3 October   1999     0     0     2     2
4 February  2001     2     0     0     2
5 June      2001     0     2     1     3
6 November  2001     0     0     2     2

数据

data <- structure(list(Date = c("15/2/1999", "15/2/1999", "15/2/1999", 
"15/5/1999", "15/5/1999", "15/10/1999", "15/10/1999", "15/2/2001", 
"15/2/2001", "15/6/2001", "15/6/2001", "15/6/2001", "15/11/2001", 
"15/11/2001"), Cat = c("A", "A", "B", "A", "B", "C", "C", "A", 
"A", "B", "B", "C", "C", "C")), class = "data.frame", row.names = c(NA, 
-14L))

另一种可能的解决方案:

library(tidyverse)
library(lubridate)
df <- data.frame(
stringsAsFactors = FALSE,
Date = c("15/2/1999",
"15/2/1999","15/2/1999","15/5/1999","15/5/1999",
"15/10/1999","15/10/1999","15/2/2001","15/2/2001",
"15/6/2001","15/6/2001","15/6/2001","15/11/2001",
"15/11/2001"),
Cat = c("A","A","B","A",
"B","C","C","A","A","B","B","C","C","C")
)
df %>% 
mutate(Month = month(Date, label = TRUE), Year = year(dmy(Date))) %>% 
pivot_wider(id_cols = c(Month, Year), names_from = Cat,
values_from = Cat, values_fn = length, values_fill = 0) %>% 
mutate(Total = rowSums(.[3:5]))
#> # A tibble: 6 × 6
#>   Month  Year     A     B     C Total
#>   <ord> <dbl> <int> <int> <int> <dbl>
#> 1 Feb    1999     2     1     0     3
#> 2 May    1999     1     1     0     2
#> 3 Oct    1999     0     0     2     2
#> 4 Feb    2001     2     0     0     2
#> 5 Jun    2001     0     2     1     3
#> 6 Nov    2001     0     0     2     2

最新更新