【问题标题】:Creating a new dateframe by factor in R在 R 中按因子创建新的数据框
【发布时间】:2014-07-15 17:43:40
【问题描述】:

如果我在 R 中有以下数据框

date <- rep(seq(as.Date("2013-11-1"),as.Date("2014-6-1"), by = "months"),2)
category <- c(rep("Group 1","8"),rep("Group 2","8"))
x <- c(rnorm(8), rnorm(8))
data.frame(date,category,x)

         date category          x
1  2013-11-01  Group 1 -0.5511129
2  2013-12-01  Group 1 -0.6640636
3  2014-01-01  Group 1  0.6348586
4  2014-02-01  Group 1  0.2673702
5  2014-03-01  Group 1  0.9949441
6  2014-04-01  Group 1  0.4077544
7  2014-05-01  Group 1  1.8395109
8  2014-06-01  Group 1 -0.4685328
9  2013-11-01  Group 2 -0.7624855
10 2013-12-01  Group 2 -1.1774081
11 2014-01-01  Group 2 -2.5409333
12 2014-02-01  Group 2  0.5013774
13 2014-03-01  Group 2  0.6504688
14 2014-04-01  Group 2  0.2582353
15 2014-05-01  Group 2  0.6385828
16 2014-06-01  Group 2 -0.4358158

我怎样才能产生以下输出:

  date         group 1      group 2
1  2013-11-01  -0.5511129g   
2  2013-12-01  -0.6640636    
3  2014-01-01  0.6348586
4  2014-02-01  0.2673702     group 2 variable x values here
5  2014-03-01  0.9949441     
6  2014-04-01  0.4077544
7  2014-05-01  1.8395109
8  2014-06-01 -0.4685328

我想要的是一个跨时间的数据框,第一列作为日期,其余列对应于不同组的变量 x。假设会有更多的组和变量。我希望这是有道理的!

【问题讨论】:

    标签: r time-series reshape


    【解决方案1】:

    这是您的标准票价从“长”到“宽”的重塑问题。

    从“reshape2”看dcast:

    set.seed(1)
    date <- rep(seq(as.Date("2013-11-1"),as.Date("2014-6-1"), by = "months"),2)
    category <- c(rep("Group 1","8"),rep("Group 2","8"))
    x <- c(rnorm(8), rnorm(8))
    mydf <- data.frame(date,category,x)
    
    library(reshape2)
    dcast(mydf, date ~ category)
    # Using x as value column: use value.var to override.
    #         date    Group 1     Group 2
    # 1 2013-11-01 -0.6264538  0.57578135
    # 2 2013-12-01  0.1836433 -0.30538839
    # 3 2014-01-01 -0.8356286  1.51178117
    # 4 2014-02-01  1.5952808  0.38984324
    # 5 2014-03-01  0.3295078 -0.62124058
    # 6 2014-04-01 -0.8204684 -2.21469989
    # 7 2014-05-01  0.4874291  1.12493092
    # 8 2014-06-01  0.7383247 -0.04493361
    

    或者,在基数 R 中,reshape:

    reshape(mydf, direction = "wide", idvar = "date", timevar = "category")
    #         date  x.Group 1   x.Group 2
    # 1 2013-11-01 -0.6264538  0.57578135
    # 2 2013-12-01  0.1836433 -0.30538839
    # 3 2014-01-01 -0.8356286  1.51178117
    # 4 2014-02-01  1.5952808  0.38984324
    # 5 2014-03-01  0.3295078 -0.62124058
    # 6 2014-04-01 -0.8204684 -2.21469989
    # 7 2014-05-01  0.4874291  1.12493092
    # 8 2014-06-01  0.7383247 -0.04493361
    

    而且,为了多样化,还有另一种方法 :-)

    library(dplyr)
    library(tidyr)
    mydf %>% spread(category, x)
    

    【讨论】:

    • 嘿,这很好,但是如果我们有更多变量,例如 x.1、x.2、x.3 等,我们如何应用这些工具。
    • @theamaturdataanalyst, reshape 应该开箱即用。对于“reshape2”和“tidyr”方法,您首先需要将数据转换为“长”形式。在“reshape2”中,术语是melt,而在“tidyr”中,术语是gather。
    猜你喜欢
    • 1970-01-01
    • 2014-12-31
    • 2018-09-23
    • 1970-01-01
    • 1970-01-01
    • 2016-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多