【问题标题】:Melting/casting my data into shape将我的数据熔化/铸造成形状
【发布时间】:2011-05-28 03:36:02
【问题描述】:

我有一张如下所示的表格:

我需要它看起来像这样,其中 net=gross-tare:

我该怎么做?


我首先融合数据,然后转换为列,然后为净读数创建新列。

  df_m <- melt(df, id = 1:3)
  df_c <- cast(df_m, ... ~ variable + type)

  df_c$wr_net  <- df_c$wr_gross  - df_c$wr_tare
  df_c$wc_net  <- df_c$wc_gross  - df_c$wc_tare
  df_c$tsa_net <- df_c$tsa_gross - df_c$tsa_tare

这给了

但现在我不知道如何融合此表以使数据框看起来像我需要的“类型”列,其值为“毛”、“皮重”和“净”。

有没有更简单的方法?我是不是用熔化/施法叫错了树?


您可以使用此复制我的数据的小样本...

  df <- structure(list(train = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "AC0485n", class = "factor"), 
    position = c(1L, 1L, 2L, 2L, 3L, 3L), type = structure(c(2L, 
    1L, 2L, 1L, 2L, 1L), .Label = c("gross", "tare"), class = "factor"), 
    wids_raw = c(24.85, 146.2, 26.16, 135, 24.7, 135.1), wids_corr = c(26.15, 
    145.43, 27.44, 134.43, 26, 134.52), tsa = c(24.1, 139.2, 
    25, 133.6, 24, 131.1)), .Names = c("train", "position", "type", 
    "wr", "wc", "tsa"), class = "data.frame", row.names = c(NA, 
    -6L))

【问题讨论】:

    标签: r reshape


    【解决方案1】:

    如果你真的想只使用 reshape 来做,我会这样做:

    library(reshape2)
    
    df_m <- melt(df, id = 1:3)
    
    df_c <- dcast(df_m, ... ~  type)
    df_c$net <- df_c$gross  - df_c$tare
    
    df_m2 <- melt(df_c, 1:3)
    names(df_m2)[4] <- "type"
    
    dcast(df_m2, ... ~ variable)
    

    【讨论】:

    • 有reshape2吗?惊人的!现在只是浏览文档。
    【解决方案2】:

    我认为你只需要使用 ddply:

    ddply(df,.(position),.fun=function(x){
    newrow <- x[1,]
    newrow$type <- "net"
    newrow[4:6] <- x[x$type=="gross",4:6] - x[x$type=="tare",4:6]
    return(rbind(x,newrow))
    })
    

    返回,

       train   position  type     wr     wc   tsa
     1 AC0485n        1  tare  24.85  26.15  24.1
     2 AC0485n        1 gross 146.20 145.43 139.2
     3 AC0485n        1   net 121.35 119.28 115.1
     4 AC0485n        2  tare  26.16  27.44  25.0
     5 AC0485n        2 gross 135.00 134.43 133.6
     6 AC0485n        2   net 108.84 106.99 108.6
     7 AC0485n        3  tare  24.70  26.00  24.0
     8 AC0485n        3 gross 135.10 134.52 131.1
     9 AC0485n        3   net 110.40 108.52 107.1
    

    编辑: 如果你真的想使用熔化/铸造,我认为这很有效:

    dd <-  melt.data.frame(df_c,id.vars=1:2)
    dd$type <- factor(do.call("rbind",strsplit(as.character(dd$variable),"_"))[,2])
    dd$variable <- factor(do.call("rbind",strsplit(as.character(dd$variable),"_"))[,1])
    

    【讨论】:

    • 嘿,你知道吗!我喜欢。虽然我仍然很好奇如何使用cast 和melt 来完成它。任何人都可以对此发表评论吗?
    • 我只是用我的实际数据框试了一下(有 16 列,其中 wr、wc、tsa 是最后三列。我已将代码更改为newrow[,14:16] &lt;- x[x$type=="gross",14:16] - x$type=="tare",14:16],但它给出了一个错误@ 987654327@. 14:16 是正确的,因为 df[,14:16] 显示了 wr、wc、tsa 列。
    • 抱歉,我认为我的代码中有一个小错字,可能会导致它无法在任何情况下工作。我把它固定在上面。尝试将 newrow[,14:16] 更改为 newrow[14:16]。我使用熔化/铸造添加了一个潜在的解决方案。
    • 谢谢,你的融化/铸造作品!不幸的是,带有 [14:16] 的 ddply 仍然给出关于 equally-sized data frames 的相同错误。知道什么给出了吗?
    • 我刚刚对我的数据做了一个汇总,有 67694 type = "gross" 和 67919 type = "tare"。这可能是原因吗?我想我必须删除一些 type = "tare" 值。
    猜你喜欢
    • 2019-02-08
    • 2014-07-12
    • 1970-01-01
    • 1970-01-01
    • 2014-02-25
    • 2017-06-24
    • 2016-12-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多