【问题标题】:R - Reshape - Melt ErrorR - 重塑 - 熔体错误
【发布时间】:2012-12-20 14:10:41
【问题描述】:

我正在尝试融化一个数据框,但我收到了这个奇怪的错误。任何想法为什么?

str(zx7)
'data.frame':   519 obs. of  5 variables:
 $ calday.new: Date, format: "2011-01-03" "2011-01-04" "2011-01-05" "2011-01-06" ...
 $ A20    : Time-Series  from 1 to 519: 0 0 0 0 0 0 0 0 0 0 ...
 $ B20    : Time-Series  from 1 to 519: 0 0 0 0 0 0 0 0 0 0 ...
 $ C20    : Time-Series  from 1 to 519: 0 0 0 0 0 0 0 0 0 0 ...
 $ D20    : Time-Series  from 1 to 519: 0 0 0 0 0 0 0 0 0 0 ...

zx7.melt <- melt(zx7, id=c("calday.new"))
Error in `[<-.ts`(`*tmp*`, ri, value = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  : only replacement of elements is allowed

【问题讨论】:

  • @你使用哪个版本的 reshape2?我无法重现错误。
  • @agstudy,正确的问题是“你使用哪个版本的重塑?”查看我的回复。

标签: r reshape melt


【解决方案1】:

我不知道你是如何创建你的结构的,但是当我这样做时,它对我有用

zx7 <- data.frame( calday.new=seq(from = as.Date('2011-01-03'),by=1,length.out=519),
                   A20=ts(rep(0,519)),
                   B20=ts(rep(0,519)),
                   C20=ts(rep(0,519)),
                   D20=ts(rep(0,519)))

我创建了与上面相同的结构:

str(zx7)
'data.frame':   519 obs. of  5 variables:
 $ calday.new: Date, format: "2011-01-03" "2011-01-04" "2011-01-05" ...
 $ A20       : Time-Series  from 1 to 519: 0 0 0 0 0 0 0 0 0 0 ...
 $ B20       : Time-Series  from 1 to 519: 0 0 0 0 0 0 0 0 0 0 ...
 $ C20       : Time-Series  from 1 to 519: 0 0 0 0 0 0 0 0 0 0 ...
 $ D20       : Time-Series  from 1 to 519: 0 0 0 0 0 0 0 0 0 0 ...

然后我就融化了:

head(melt(zx7, id=c("calday.new")))
  calday.new variable value
1 2011-01-03      A20     0
2 2011-01-04      A20     0
3 2011-01-05      A20     0
4 2011-01-06      A20     0
5 2011-01-07      A20     0
6 2011-01-08      A20     0

【讨论】:

  • 您也得到了我的 +1,因为您花时间重新创建了数据集,并且您对原始问题的评论在我的脑海中引发了一个标志:“谁对 'reshape2' 说过任何话?”干得好。
【解决方案2】:

问题是旧的“reshape”包的melt()函数在遇到类ts的对象时不知道该怎么做。

因此,您有两个明显的选择(尽管可能还有更多):

  1. unclass 在你melt() 你的数据之前当前被归类为ts 的变量:

    zx7b <- zx7         # Make a backup, just in case
    library(reshape)    # Notice this is "reshape", not "reshape2"
    head(melt(zx7b, id=c("calday.new"))) # Doesn't work
    # Error in `[<-.ts`(`*tmp*`, ri, value = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  : 
    #   only replacement of elements is allowed
    
    ## Unclass the relevant columns from your data.frame
    zx7b[sapply(zx7b, is.ts)] <- lapply(zx7b[sapply(zx7b, is.ts)],
                                        unclass)
    head(melt(zx7b, id=c("calday.new")))
    #   calday.new variable value
    # 1 2011-01-03      A20     0
    # 2 2011-01-04      A20     0
    # 3 2011-01-05      A20     0
    # 4 2011-01-06      A20     0
    # 5 2011-01-07      A20     0
    # 6 2011-01-08      A20     0
    
  2. 改为升级到“reshape2”,不需要取消分类。

    library(reshape2) # Notice that this is reshape2!
    head(melt(zx7, id=c("calday.new"))) # Melt the original data.frame
    #   calday.new variable value
    # 1 2011-01-03      A20     0
    # 2 2011-01-04      A20     0
    # 3 2011-01-05      A20     0
    # 4 2011-01-06      A20     0
    # 5 2011-01-07      A20     0
    # 6 2011-01-08      A20     0
    

我没有花时间去做,但是您可以检查每个版本的“reshape”包中melt()melt.data.frame 方法的代码,看看差异在哪里。安装这两个包,然后输入reshape2:::melt.data.framereshape:::melt.data.frame 以查看底层功能。

【讨论】:

  • +1!首先,我总是发现在包名中添加一个数字(reshape、rowygen、..)不是很优雅并且是混乱的根源!我希望这将被弃用以避免此类错误!其次我会叫你重塑先生!你甚至说服我掌握了基础包的reshape功能!
  • @agstudy,我同意更改包的名称。我理解为什么这样做了,但是拥有两个功能和语法相似但不相同的包可能会令人困惑。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-29
相关资源
最近更新 更多