2012-10-10 44 views
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

我認爲有一個相當簡單的解決方案,但我不能弄清楚。您的建議非常感謝!

回答

1
library(plyr) 
# assuming lotsadates has 2 columns, Date and day_of_week 
ndist <- ddply(lotsadates, .(Date, day_of_week), summarise, n=length(Date)) 
+0

這很容易做到。非常感謝! – SCallan

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 
+0

音符data.table方法:一定要'cbind'了'day_of_week'列到你的'lotsadates' data.frame這個工作。 – Maiasaura

+0

謝謝!這工作。 – SCallan

3

編碼優雅

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)] 
相關問題