【问题标题】:How to pivot a table to make columns fro a variable row values in R如何透视表以从R中的可变行值生成列
【发布时间】:2011-09-22 05:01:00
【问题描述】:

我有一个包含以下列的 data.frame:Month、Store 和 Demand。

Month   Store   Demand
Jan     A   100
Feb     A   150
Mar     A   120
Jan     B   200
Feb     B   230
Mar     B   320

我需要旋转它以创建一个新的 data.frame 或数组,每个月都有列,存储例如:

Store   Jan Feb Mar
A       100 150 120
B       200 230 320

非常感谢任何帮助。我刚开始使用 R。

【问题讨论】:

    标签: r pivot reshape


    【解决方案1】:
    > df <- read.table(textConnection("Month   Store   Demand
    + Jan     A   100
    + Feb     A   150
    + Mar     A   120
    + Jan     B   200
    + Feb     B   230
    + Mar     B   320"), header=TRUE)
    

    因此,您的“月份”列很可能是一个按字母顺序排序的因素 (编辑:)

    > df$Month <- factor(df$Month, levels= month.abb[1:3])
     # Just changing levels was not correct way to handle the problem. 
     # Need to use within a factor(...) call.
    > xtabs(Demand ~ Store+Month, df)
          Month
     Store Jan Feb Mar
         A 100 150 120
         B 200 230 320
    

    一个不太明显的方法(因为'I'函数返回它的参数):

    > with(df, tapply(Demand, list(Store, Month) , I)  )
      Jan Feb Mar
    A 100 150 120
    B 200 230 320
    

    【讨论】:

    • 您的答案中的打印输出不太正确。例如,Jan A 应该是 100 而不是 150。您的代码有效,所以我怀疑您在更改关卡之前复制了输出。
    • 您对错误是正确的,但这是因为我不正确地更改了级别。见上文。
    【解决方案2】:

    欢迎来到 R。

    通常有很多方法可以使用 R 达到相同的目的。另一种方法是使用 Hadley 的 reshape 包。

    # create the data as explained by @Dwin
    df <- read.table(textConnection("Month   Store   Demand
                                    Jan     A   100
                                    Feb     A   150
                                    Mar     A   120
                                    Jan     B   200
                                    Feb     B   230
                                    Mar     B   320"), 
                     header=TRUE)
    
    # load the reshape package from Hadley -- he has created GREAT packages
    library(reshape)
    
    # reshape the data from long to wide
    cast(df, Store ~ Month)
    

    作为参考,您应该查看这个很棒的教程。 http://www.jstatsoft.org/v21/i12/paper

    【讨论】:

    • 如果不重新排序不会给 OP 他所要求的东西的关卡。
    【解决方案3】:

    如果数据在dat 中(并且级别设置为日历顺序),那么另一个基本 R 解决方案是使用(非常不直观)reshape() 函数:

    reshape(dat, v.names = "Demand", idvar = "Store", timevar = "Month", 
            direction = "wide")
    

    数据的sn-p给出:

    > reshape(dat, v.names = "Demand", idvar = "Store", timevar = "Month", 
    +         direction = "wide")
      Store Demand.Jan Demand.Feb Demand.Mar
    1     A        100        150        120
    4     B        200        230        320
    

    如果您愿意,可以轻松清除名称:

    > out <- reshape(dat, v.names = "Demand", idvar = "Store", timevar = "Month", 
    +                direction = "wide")
    > names(out)[-1] <- month.abb[1:3]
    > out
      Store Jan Feb Mar
    1     A 100 150 120
    4     B 200 230 320
    

    (为了获得上面的输出,我以与@DWin's Answer 中显示的类似方式读取数据,然后运行以下命令:

    dat <- transform(dat, Month = factor(Month, levels = month.abb[1:3]))
    

    dat 是我所说的数据)

    【讨论】:

    • +1 只是为了重申我在使用重塑功能时的挫败感。
    猜你喜欢
    • 1970-01-01
    • 2019-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-03
    • 2011-01-30
    • 1970-01-01
    相关资源
    最近更新 更多