【问题标题】:Including dates with zero rows in ddply在 ddply 中包含零行的日期
【发布时间】:2013-02-24 00:19:33
【问题描述】:

我正在使用 plyr 包中的 ddply 按日期获取行数。但是,当有没有值的日期时,它们会被忽略,我最终会得到一个不存在某些日期的数据框。我正在使用 ddply 生成的值来生成一个绘图,并想找到如何包含所有不存在的日期,并给它们一个 nrow 值 0。

library(plyr)
f = ddply(df, .(created), "nrow")
f = as.data.frame(f)

> head(f)
     created nrow
1 2009-12-29    2
2 2009-12-30    3
3 2010-01-06    1
4 2010-01-07    2
5 2010-01-08    2
6 2010-01-11    1

为了正确构建线图,我想要所有日期(那些具有 0 nrow 值的日期)。因此,在上述部分中,我想将所有缺失的日期(例如“2010-01-01”)添加到“2010-01-05”,并为每个日期赋予 9 的 nrow 值。没有人能提出一种优雅的方式来执行此操作任务。

ggplot(f, aes(x=created, y=nrow)) + 
  geom_line(size=0.6, color="darkgreen") +
  labs(title="Plot") +
  theme(axis.text.y=element_text(family="sans", face="bold"),  
        axis.text.x=element_text(family="sans", face="bold"))  

我以为 ddply 中的 .drop 命令执行了这个任务,但它似乎没有这样做。

编辑:

样本数据。

mdf=data.frame(created=c('2009-12-29','2009-12-30','2010-01-06','2010-01-07',
                     '2010-01-08','2010-01-11','2009-12-29','2009-12-30'))

ddply(mdf, .(created), .drop=FALSE, "nrow")



    created nrow
1 2009-12-29    2
2 2009-12-30    2
3 2010-01-06    1
4 2010-01-07    1
5 2010-01-08    1
6 2010-01-11    1

如何将 '2010-01-01'、'2010-01-02' 等包含在 nrow 的值为 0 中。

【问题讨论】:

  • 能否提供样本df 让我有时间自己构建数据?

标签: r


【解决方案1】:

使用适当类的对象

由于您想要情节,因此将日期提供为“日期”类就足够了。

# Load libraries
library(package=plyr)
library(package=ggplot2)

# Create data
mdf <- data.frame(created=c('2009-12-29','2009-12-30','2010-01-06','2010-01-07',
                     '2010-01-08','2010-01-11','2009-12-29','2009-12-30'))
mdf$created <- as.Date(mdf$created)

# Plot with variable of class "Date"
ggplot(mdf, aes(x=created)) + 
  geom_line(size=0.6, color="darkgreen", stat='bin', binwidth=1) +
  labs(title="Plot") +
  theme(axis.text.y=element_text(family="sans", face="bold"),  
        axis.text.x=element_text(family="sans", face="bold"))

【讨论】:

    【解决方案2】:

    一种直接的方法是使用来自ddply 的结果的minmax 创建另一个data.frame

    # dummy data.frame
    set.seed(45)
    dates <- seq(as.Date("2013-01-01"), as.Date("2013-03-31"), by=3)
    df <- data.frame(created=sample(dates, 100, replace=T))
    # your plyr result
    require(plyr)
    df.r <- ddply(df, .(created), nrow) # 30 * 2
    
    # solution:
    df2 <- data.frame(created = seq(min(df.r$created), max(df.r$created), by=1), V1 = 0)
    idx <- match(df2$created, df.r$created)
    df2$V1[!is.na(idx)] <- df.r$V1[idx[!is.na(idx)]]
    

    现在,df2 将包含 0's 的所有日期,这些日期在 df.r 中缺失。我不确定这是否“优雅”!

    【讨论】:

      【解决方案3】:

      您可以预先准备一个包含所有日期(序列)的列表,然后与您的表格日期合并。这将在原始数据中插入 NA。

      dd <- ddply(mdf,.(created),nrow)
      df.miss <- data.frame(created=seq.Date(min(dd$created),max(dd$created),1))
      dat <- merge(dd,df.miss,all.y=T)
      dat[is.na(dat)] <- 0  ## I replace NA by 0 here
      

      现在我绘制我的数据

      library(lattice)
      xyplot(V1~created,data=dat, type=c('l','p'),cex=2,lty=2)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多