0

我的数据框“lotsadates”中的数据如下所示:

>Date

1 2012-09-26
2 2012-09-26
3 2012-09-26
4 2012-09-27
5 2012-09-28
6 2012-09-28

有一个相同长度的 day_of_week 向量:

> day_of_week

1 3
2 3
3 3
4 4
5 5
6 5

我正在使用以下方法按日期计算观察次数:

ndist <-tapply(1:NROW(lotsadates), 
               lotsadates$Date, 
               function(x) length(unique(x)))

所以 ndist 看起来像这样:

观察 / 日期 / ndist
1 / 2012-09-26 / 3
2 / 2012-09-27 / 1
3 / 2012-09-28 / 2

但我希望 ndist 看起来像这样:

日期 / ndist / day_of_week
1 / 2012-09-26 / 3 / 3
2 / 2012-09-27 / 1 / 4
3 / 2012-09-28 / 2 / 5

我认为有一个相当简单的解决方案,但我无法弄清楚。非常感谢您的建议!

4

3 回答 3

3

一种data.table编码优雅的方法

library(data.table)
# assuming lotsadates has 2 columns, Date and day_of_wee
DT <- as.data.table(lotsadates)
DT[, .N, by = list(Date, day_of_week)]
于 2012-10-16T05:20:16.090 回答
2
library(reshape2)
result <- dcast(lotsadates, Date ~., value.var='day_of_week')
result$day_of_week <- as.POSIXlt(result$Date)$wday
names(result)[2] <- "ndist"
> result
        Date ndist day_of_week
1 2012-09-26     3           3
2 2012-09-27     1           4
3 2012-09-28     2           5
于 2012-10-10T22:50:30.840 回答
1
library(plyr)
# assuming lotsadates has 2 columns, Date and day_of_week
ndist <- ddply(lotsadates, .(Date, day_of_week), summarise, n=length(Date))
于 2012-10-11T00:53:56.977 回答