【问题标题】:Aggregate data for 3 months for the current year and then transpose the uniques values of a column in R汇总当年 3 个月的数据,然后转置 R 中列的唯一值
【发布时间】:2015-02-11 03:44:28
【问题描述】:

我有一列表示数据框中的日期。我只是想创建一个新的数据框,总结 3 个月内 ID 处的金额(20140831、20140930、20141031) 使用日期列。然后用每个汇总的金额值转置品牌列。最好的方法是什么?

数据集如下。

  ID         date       Brand     Amount
 1001   20141031    UNIBIC          9.8
 1001   20140930    UNIBIC          1.023
 1002   20140831    CITRIZINE       2.019
 1002   20140930    CITRIZINE       2.015
 1002   20141031    CITRIZINE       1.002
 1003   20140831    CHOCO           4.22
 1004   20140930    SOLOSTAR        1.007
 1004   20141030    SOLOSTAR        1.008
 1005   20140930    DOLO            1.025

我希望得到如下输出

  ID           UNIBIC     CITRIZINE   CHOCO   SOLOSTAR      DOLO  
  1001        5.4115                
  1002                  1.678
  1003                               4.22        
  1004                                        1.039
  1005                                                    1.025

我们将不胜感激您提供的任何帮助

【问题讨论】:

    标签: r


    【解决方案1】:

    你可以试试

    library(reshape2)
    df$Brand <- factor(df$Brand, levels=unique(df$Brand))
    dcast(df, ID~Brand, value.var='Amount', mean)
    #   ID UNIBIC CITRIZINE CHOCO SOLOSTAR  DOLO
    #1 1001 5.4115       NaN   NaN      NaN   NaN
    #2 1002    NaN  1.678667   NaN      NaN   NaN
    #3 1003    NaN       NaN  4.22      NaN   NaN
    #4 1004    NaN       NaN   NaN   1.0075   NaN
    #5 1005    NaN       NaN   NaN      NaN 1.025
    

    或者你可以试试dcast.data.table,这样会更快

    library(data.table) 
    dcast.data.table(setDT(df), ID~Brand, value.var='Amount', mean)
    

    或者使用dplyr/tidyr

    library(dplyr)
    library(tidyr)
    
    df %>%
       group_by(ID, Brand) %>% 
       summarise(Amount=mean(Amount)) %>%
       ungroup() %>%
       spread(Brand, Amount)
    

    更新

    如果您只需要AugSepOct,您可以在转换前subset 数据集。

    df1 <-  df[as.numeric(substr(df$date, 5,6)) %in% 8:10,]
    dcast(df1, ID~Brand, value.var='Amount', mean)
    

    数据

     df <- structure(list(ID = c(1001L, 1001L, 1002L, 1002L, 1002L, 1003L, 
     1004L, 1004L, 1005L), date = c(20141031L, 20140930L, 20130831L, 
     20140930L, 20141031L, 20130831L, 20130930L, 20131030L, 20140930L
     ), Brand = c("UNIBIC", "UNIBIC", "CITRIZINE", "CITRIZINE", "CITRIZINE", 
     "CHOCO", "SOLOSTAR", "SOLOSTAR", "DOLO"), Amount = c(9.8, 1.023, 
     2.019, 2.015, 1.002, 4.22, 1.007, 1.008, 1.025)), .Names = c("ID", 
     "date", "Brand", "Amount"), class = "data.frame", row.names = c(NA, -9L))
    

    【讨论】:

    • 感谢 Akrun 的回复,我想要 ID:1001 的 3 个月(即 2014 年 8 月、9 月、10 月)的汇总数据,UNIBIC 应该是 5.41
    • @akrun UNIBIC 的汇总值仅为两个月,因此我输入了 5.4115。
    • @user3719979 你能告诉我你是如何得到5.41mean(df$Amount[1:2])#[1] 5.4115的计算方法
    • 再次感谢您的回复。该数据集的值从 2014 年 1 月到 2014 年 10 月,但我只需要 2014 年 8 月、2 月和 10 月的数据。我是 Unibic 的情况,它仅适用于 AUg 和 SEP,所以我对 Amount 做了一个平均值以获得 5.41
    • @user3719979 我根据您的描述更新了帖子。请检查。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多