【问题标题】:Format/Generate new table in R using dplyr/tidyr使用 dplyr/tidyr 在 R 中格式化/生成新表
【发布时间】:2018-07-07 05:47:03
【问题描述】:

我有一个使用 df 计算的表格,如下所示。

 Month_considered   pct `ATC Count`
   <fct>            <dbl> <fct>      
 1 Apr-17            54.9 198,337    
 2 May-17            56.4 227,681    
 3 Jun-17            58.0 251,664    
 4 Jul-17            57.7 251,934    
 5 Aug-17            55.5 259,617    
 6 Sep-17            55.7 245,588    
 7 Oct-17            56.6 247,051    
 8 Nov-17            57.6 256,375    
 9 Dec-17            56.9 277,784    
10 Jan-18            56.7 272,818  

现在我想找出两个月之间的 pct 差异。所以想要的输出应该是这样的

 Month_considered          pct 
   <fct>                   <dbl>    
 1 Apr-17-May-17            1.5    
 2 May-17-Jun-17            1.6   
 3 Jun-17-Jul-17           - 0.3  

如何像上面那样连接第一列。我确实尝试在tidyr 中使用unite,但这不是我想要生成的输出。谢谢。

【问题讨论】:

    标签: r dplyr tidyr


    【解决方案1】:

    我们需要取当前值和下一个值之间的差

    library(dplyr)
    library(zoo)
    df1 %>%
        arrange(as.yearmon(Month_considered, format = "%b-%y")) %>% # to order
        mutate_at(vars(Month_considered, pct),
                  funs(new = lead(., default = last(.)))) %>% 
        unite(Month_considered, Month_considered, Month_considered_new, sep="-") %>% 
        transmute(Month_considered, pct = pct_new - pct)
    #   Month_considered  pct
    #1     Apr-17-May-17  1.5
    #2     May-17-Jun-17  1.6
    #3     Jun-17-Jul-17 -0.3
    #4     Jul-17-Aug-17 -2.2
    #5     Aug-17-Sep-17  0.2
    #6     Sep-17-Oct-17  0.9
    #7     Oct-17-Nov-17  1.0
    #8     Nov-17-Dec-17 -0.7
    #9     Dec-17-Jan-18 -0.2
    #10    Jan-18-Jan-18  0.0
    

    或使用base R

    pct <- df1$pct[-1] - df1$pct[-nrow(df1)]
    Month_considered <- paste(df1$Month_considered[-1], 
                 df1$Month_considered[-nrow(df1)], sep="-")
    
    data.frame(Month_considered, pct)
    

    【讨论】:

      【解决方案2】:

      zoo::yearmon类型列添加1个月后,可以尝试使用self-join

      要在yearmon 类型列中添加月份,只需添加1/12

      解决办法是:

      library(zoo)
      library(dplyr)
      
      df %>% mutate(Month_considered = as.yearmon(Month_considered, "%b-%y"), 
                    Next_Month = Month_considered+(1/12)) %>%
        #self join
        left_join(.,.,by=c("Next_Month"="Month_considered")) %>%
        mutate(Month_considered = paste(Month_considered,Next_Month,sep="-"), 
               pct = pct.y - pct.x) %>%
        select(Month_considered, pct)
      
      #     Month_considered  pct
      # 1  Apr 2017-May 2017  1.5
      # 2  May 2017-Jun 2017  1.6
      # 3  Jun 2017-Jul 2017 -0.3
      # 4  Jul 2017-Aug 2017 -2.2
      # 5  Aug 2017-Sep 2017  0.2
      # 6  Sep 2017-Oct 2017  0.9
      # 7  Oct 2017-Nov 2017  1.0
      # 8  Nov 2017-Dec 2017 -0.7
      # 9  Dec 2017-Jan 2018 -0.2
      # 10 Jan 2018-Feb 2018   NA
      

      数据:

      df <- read.table(text=
      "Month_considered   pct 'ATC Count'
      Apr-17            54.9 198337    
      May-17            56.4 227681    
      Jun-17            58.0 251664    
      Jul-17            57.7 251934    
      Aug-17            55.5 259617    
      Sep-17            55.7 245588    
      Oct-17            56.6 247051    
      Nov-17            57.6 256375    
      Dec-17            56.9 277784    
      Jan-18            56.7 272818",
      header=TRUE, stringsAsFactors = FALSE)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-10-08
        • 2016-12-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-18
        • 1970-01-01
        • 2018-05-07
        相关资源
        最近更新 更多