使用R:
最初,表看起来是: PS (日期列的格式是:(dd/mm/yyyy))
Date Q Season
01/01/2017 1023 Winter
01/02/2017 2233 Winter
01/03/2017 1312 Winter
01/04/2017 1194 Spring
01/05/2017 907 Spring
01/06/2017 1939 Spring
01/07/2017 3264 Summer
01/08/2017 2232 Summer
01/09/2017 3574 Summer
01/10/2017 1533 Fall
01/11/2017 907 Fall
01/12/2017 1778 Fall同样,2018年和2019年(不同产品可以更改,因此下表需要在行数方面是动态的)
我想让这张桌子像:
Date Winter Spring Summer Fall
2017 Sum of Q (01/17-03/17) Sum of Q (04/17-06/17) Sum of Q (07/17-09/17) Sum of Q (10/17-12/17)
2018 Sum of Q (01/18-03/18) Sum of Q (04/18-06/18) Sum of Q (07/18-09/18) Sum of Q (10/18-12/18)
2019 Sum of Q (01/19-03/19) Sum of Q (04/19-06/19) Sum of Q (07/19-09/19) Sum of Q (10/19-12/19)I/P数据:
结构(时间=结构(1:30,.Label =c)(“2017-02-01”,"2017-03-01","2017-04-01","2017-05-01","2017-06-01","2017-07-01","2017-08-01","2017-09-01","2017-10-01","2017-11-01","2017-12-01",“2018-01”,"2018-02-01“、"2018-03-01”、"2018-04-01“、"2018-05-01”、"2018-06-01“、"2018-07-01”、"2018-08-01“、"2018-09-01”、"2018-10-01“、"2018-11-01”、"2018-12-01“、”2019-01“、"2019-02-01”、“2019-02-01”、"2019-03-01","2019-04-01“、"2019-05-01”、"2019-06-01“、”2019-07-01“、类= "factor")、Qty = c(1638.2520529、0、0、1644.9257156、1629.2808217、2227.0057667、586.0942485、593.1155226、1664.6943283、2193.6993566、2696.5906737、3354.9333327、2749.2299918、2749.2299918、2791.6314399、2145.3504203、3311.5319066、4446.0188521、2740.95081、2786.6458353、3839.8798065、3216.524473、566.3303976,0,0,0,559.2043639,0,528.4764685 ),季度= c("Q1","Q1","Q2","Q3","Q4","Q1","Q2","Q3","Q4","Q4“、"Q4”、"Q1“、"Q2”、"Q3"),季节=结构( 1L,2L,2L,2L,3L,3L,3L,4L,4L,4L,1L,1L,1L,1L,2L,2L,2L,3L,3L,3L,4L,4L,1L,1L,1L,2L,2L,3L,3L,3L),.Label =c(“冬天”,“春天”),),row.names = c(NA,-30L),class = "data.frame")
发布于 2020-07-08 04:47:03
将Date更改为date类,从中提取年份并将数据转换为宽格式。
library(dplyr)
df %>%
mutate(Time = as.Date(Time),
year = format(Time, '%Y')) %>%
select(-Time, -Quarters) %>%
tidyr::pivot_wider(names_from = Season, values_from = Qty, values_fn = sum)
#OR in old tidyr
#tidyr::pivot_wider(names_from = Season, values_from = Qty,
# values_fn = list(Qty = sum))
# A tibble: 3 x 5
# year Winter Spring Summer Fall
# <chr> <dbl> <dbl> <dbl> <dbl>
#1 2017 4335. 1645. 4442. 4452.
#2 2018 6670. 8773. 10498. 9843.
#3 2019 0 559. 528. NA 另一种选择,不是使用values_fn,而是可以先使用sum,然后将其转换为wide。
df %>%
type.convert(as.is = TRUE) %>%
mutate(Time = as.Date(Time),
year = format(Time, '%Y')) %>%
select(-Time, -Quarters) %>%
group_by(year, Season) %>%
summarise(Qty = sum(Qty)) %>%
tidyr::spread(Season, Qty)
#OR
#tidyr::pivot_wider(names_from = Season, values_from = Qty)https://stackoverflow.com/questions/62787685
复制相似问题