为什么包含.Dim属性会导致d_ply失败?

时间:2016-02-25 18:30:40

标签: r plyr

为什么包含.Dim属性会导致d_ply失败?

library(plyr)

dt <- structure(
    c(946771200, 947376000, 947980800)
    ,.Dim = 3L # FIXME: including the .Dim attribute will cause d_ply to fail
    ,class = c("POSIXct","POSIXt")
    ,tzone = "")

myDF <- data.frame( 
    dt = dt
    ,dat = seq(1:3))
#attr( myDF$dt, "dim" ) <- NULL # FIXME: removing the .Dim attribute will make d_ply work again
attr( myDF$dt, "dim" )

d_ply( .data = myDF, .variables = .(dt), .fun = show )

返回

Error in attributes(out) <- attributes(col) : 
  dims [product 3] do not match the length of object [1]

myDFstr(myDF)看起来完全没问题。当我试图创建一个可重现的例子时,我开始:

myDF <- data.frame(
    dt = as.POSIXct( c("2000-01-01", "2000-01-08", "2000-01-15") ) 
    ,dat = seq(1:3)
    ) 
d_ply( .data = myDF, .variables = .(dt), .fun = show )

......工作得很好。在找到dput属性之前,我必须attr(或.Dim)我的数据集。

我正在看这个的原因是因为我有一个函数可以按周聚合每个日期并返回一个包含这个'.Dim'属性的向量。

dt <- c( 
    seq( as.POSIXct( '2001-01-01'), by = '5 days', length.out = 52 ))    
weekCutoffPoints <- cut( dt, breaks ="week", labels = FALSE )    
endpoints <- 
    # R Inferno 8.3.22: "by is for data frames", but I get the same result with tapply
    #tapply( X = dt, INDEX = weekCutoffPoints, FUN = max )
    by( data = dt, INDICES = weekCutoffPoints, FUN = max )    
dt <- as.POSIXct( 
    endpoints[ rownames(endpoints)[weekCutoffPoints] ]
    , origin = as.POSIXct('1970-1-1') )

# now try to use these dates in the same simple d_plyr call
myDF <- data.frame( 
    dt = dt
    ,dat = seq(1:52))
#attr( myDF$dt, "dim" ) <- NULL # FIXME: removing the .Dim attribute will make d_ply work again
attr( myDF$dt, "dim" )

d_ply( .data = myDF, .variables = .(dt), .fun = show )

奖励积分:此cut / by对我有用,但这是我用这个方法在同一周内对齐日期的实际问题吗?

谢谢,伙计们!

0 个答案:

没有答案