從我的數據幀「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
我認爲有一個相當簡單的解決方案,但我不能弄清楚。您的建議非常感謝!
這很容易做到。非常感謝! – SCallan