R动态转置表最近X周

时间:2017-05-18 10:46:20

标签: r

我是R.的新人。我想实现以下目标。 我有一张桌子如下。

rowcounter  ProductNumber   YearNumber  WeekNumber  OrderedQuantity
1   735001E083155   2016    2   10
2   735001E083155   2016    3   20
3   735001E083155   2016    3   10
4   735001E083155   2016    4   60
5   735001E083155   2016    4   50
6   735001E083155   2016    5   20

我想转置此表,以便我可以将最后X个周数作为具有正确OrderQuantities for Products的列。所以一个例子就是;

rowcounter  ProductNumber   YearNumber  LastWeek Before2weeks Before3weeks 

依旧......

订购数量应该是Lastweek,Before2weeks等等...... 我计划将X切换到6到12左右。

感谢您对如何使其尽可能动态的回应。

1 个答案:

答案 0 :(得分:0)

绝对建议在tidyverse中使用dplyrtidyr,因为这会使重组数据变得非常好和自然。

我不太确定你想如何处理你的重复记录,所以我只是总结了订单数量。

library(readr)                                     
library(dplyr)                                     
library(tidyr)                                     

d <- read_csv('data')      
#> Parsed with column specification:
#> cols(
#>   rowcounter = col_integer(),
#>   ProductNumber = col_character(),
#>   YearNumber = col_integer(),
#>   WeekNumber = col_integer(),
#>   OrderedQuantity = col_integer()
#> )

d %>%                                              
## Summarize to remove duplicates                  
group_by(ProductNumber, YearNumber, WeekNumber) %>%
summarize(OrderedQuantity=sum(OrderedQuantity)) %>%
ungroup() %>%                                      

## Create header                                   
mutate(text=if_else(                               
WeekNumber==1                                      
,'Last Week'                                       
,paste0("Before", WeekNumber,"Weeks"))             
) %>%                                              

## Remove unneeded columns
## Use `filter` to remove unneeded rows                            
select(-WeekNumber) %>%                            

## Transpose                                       
spread(text, OrderedQuantity)

#> # A tibble: 1 × 6
#>   ProductNumber YearNumber Before2Weeks Before3Weeks Before4Weeks
#> *         <chr>      <int>        <int>        <int>        <int>
#> 1 735001E083155       2016           10           30          110
#> # ... with 1 more variables: Before5Weeks <int>