1
我有兩個數據框,一個包含調查日期的詳細信息,另一個包含記錄個人的實例。具體如下:基於另一個數據值的數據彙總數據
Records <- data.frame("Location"=c("A","A","B","C","C","C","D"),
"Date"= c("09/01/2017","12/01/2017","20/01/2017","06/06/2017","03/06/2017","19/01/2017","02/01/2017"),
"Individuals"= c(3,2,6,4,0,1,6))
Surveys <- data.frame("Location"=c("A","B","C","D","A","B","C","D"),
"Start"= c(rep("01/01/2017",length=4),rep("01/06/2017",length=4)),
"End"= c(rep("01/02/2017",length=4),rep("01/07/2017",length=4)))
> Surveys
Location Start End
1 A 01/01/2017 01/02/2017
2 B 01/01/2017 01/02/2017
3 C 01/01/2017 01/02/2017
4 D 01/01/2017 01/02/2017
5 A 01/06/2017 01/07/2017
6 B 01/06/2017 01/07/2017
7 C 01/06/2017 01/07/2017
8 D 01/06/2017 01/07/2017
> Records
Location Date Individuals
1 A 09/01/2017 3
2 A 12/01/2017 2
3 B 20/01/2017 6
4 C 06/06/2017 4
5 C 03/06/2017 0
6 C 19/01/2017 1
7 D 02/01/2017 6
我希望添加一列求和發生在該網站及相關時間內個體數量的調查數據幀。結果是這樣的:
Sum.Individuals <- c(5,6,1,6,0,0,4,0)
Final <- cbind(Surveys,Sum.Individuals)
> Final
Location Start End Total.Individuals
1 A 01/01/2017 01/02/2017 5
2 B 01/01/2017 01/02/2017 6
3 C 01/01/2017 01/02/2017 1
4 D 01/01/2017 01/02/2017 6
5 A 01/06/2017 01/07/2017 0
6 B 01/06/2017 01/07/2017 0
7 C 01/06/2017 01/07/2017 4
8 D 01/06/2017 01/07/2017 0
我希望這是有道理的,任何幫助表示讚賞。
乾杯
嗨,使用該方法,我得到一個只有一個觀察與所有細胞的總數的框架。同樣在我的實際數據中,記錄和調查框架很大,有很多額外的變量,所以我不確定加入它們是否可行。乾杯 – Charlie3D
加入前需要的欄目的子集。你可以使用'data.table'來進行非Equi連接。 – Gregor