搜索了兩天至目前爲止無效的解決方案。R:如何彙總數據但保留來自非彙總列的信息?
我從不同的觀察點觀察鳥類。觀察員記下物種,他們在哪裏見過它們,以及記錄多久。
現在發生的情況是,從不同的角度來看,觀測是從同一個地區得到的,但我們只想處理一個地區的每個物種的最大值。
因此,首先,我通過觀察點,品種和區域彙總的數據,並總結出了時間。
dt.agg <- aggregate(time ~ observp + species + time, dt, sum)
UUPS:完全地錯誤的命令:
應該是:
dt.agg <- aggregate(time ~ observp + species + area, dt, sum)
observp species area time
1 1a Rm A1 43.878488
2 1c Rm A1 296.152707
3 2 Rm A1 29.546790
4 1a Swm A1 34.127713
5 1b Swm A1 11.076880
6 2 Swm A1 8.771703
這個工作正常。但現在,我只需要一個地區物種的最大時間值,但是我還需要知道從哪個觀測點獲得這些數字。
在我的示例中,第2行應保留在A1中的Rm,而第1行和第3行應該被刪除。這同樣適用於第4行(保持)和5 + 6(下降)
當我剛做的品種和麪積隨着時間的推移和最大其它集合,爲觀察點的信息丟失。
有人可以告訴我一種方法來實現嗎?
乾杯
貝恩德
(現在新的帳戶,並沒有信譽..謝謝...谷歌!)
附:請隨時給這個問題一個更好的標題
UPDATE: 試圖張貼dput(head(dt,100)) - 樣本建議。原始數據集有超過1300行。希望這就是你想要的。
structure(list(species = structure(c(3L, 3L, 3L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 5L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 5L, 5L,
5L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 5L,
5L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 5L, 5L, 5L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 5L, 5L, 5L, 5L, 5L, 3L, 3L, 3L, 5L, 5L, 5L,
3L, 3L, 3L, 3L, 3L, 3L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L), .Label = c("Bf",
"Gr", "Rm", "Row", "Swm", "Wf", "Wsb", "Wst", "Ww"), class = "factor"),
area = structure(c(35L, 19L, 34L, 34L, 32L, 19L, 34L, 35L,
10L, 36L, 10L, 14L, 13L, 25L, 27L, 28L, 34L, 19L, 14L, 14L,
34L, 1L, 12L, 13L, 15L, 3L, 3L, 34L, 34L, 34L, 14L, 14L,
13L, 13L, 1L, 1L, 1L, 11L, 1L, 8L, 21L, 22L, 22L, 9L, 9L,
9L, 5L, 9L, 3L, 22L, 27L, 26L, 21L, 26L, 21L, 27L, 3L, 9L,
20L, 20L, 9L, 26L, 34L, 30L, 3L, 2L, 3L, 4L, 20L, 3L, 37L,
16L, 17L, 18L, 14L, 35L, 34L, 34L, 34L, 36L, 4L, 4L, 3L,
3L, 17L, 17L, 38L, 36L, 10L, 38L, 36L, 10L, 38L, 37L, 35L,
30L, 16L, 15L, 17L, 5L), .Label = c("A1", "A10", "A11", "A12",
"A13", "A14", "A15", "A16", "A17", "A18", "A2", "A3", "A4",
"A5", "A6", "A7", "A8", "A9", "O1", "O10", "O11", "O12",
"O13", "O14", "O15", "O16", "O17", "O18", "O19", "O2", "O20",
"O21", "O22", "O3", "O4", "O5", "O7", "O8", "O9"), class = "factor"),
observp = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L), .Label = c("1a", "1b", "1c", "2", "3", "4"), class = "factor"),
time = c(36.37086972, 2.730715967, 1.891286914, 3.782573827,
4.496276059, 5.461431934, 18.91286914, 13.22577081, 5.823001976,
5.392743201, 3.882001317, 16.97305991, 6.094384821, 5.274262222,
5.462035947, 2.089427691, 7.565147654, 21.84572774, 25.45958986,
16.97305991, 7.565147654, 4.875387532, 8.885792099, 4.062923214,
6.636122805, 7.038317277, 10.55747592, 7.565147654, 7.565147654,
3.782573827, 25.45958986, 25.45958986, 12.18876964, 12.18876964,
19.50155013, 19.50155013, 9.750775065, 39.20627398, 4.875387532,
6.423076843, 2.436283538, 1.823249104, 1.823249104, 16.72889022,
41.82222555, 33.45778044, 12.30932064, 117.1022315, 3.519158639,
1.823249104, 27.31017974, 11.11346598, 4.872567077, 11.11346598,
4.872567077, 5.462035947, 3.519158639, 16.72889022, 14.86012871,
8.916077225, 25.09333533, 22.22693195, 3.782573827, 5.184879322,
10.55747592, 8.509038411, 10.55747592, 17.70988435, 5.944051483,
3.519158639, 17.69229328, 34.70586347, 5.966017168, 3.092236431,
2.828843318, 6.612885403, 3.782573827, 3.782573827, 7.565147654,
5.392743201, 17.70988435, 17.70988435, 3.519158639, 2.346105759,
11.93203434, 11.93203434, 2.386548395, 0.898790534, 0.64700022,
2.386548395, 0.898790534, 0.64700022, 2.684866944, 6.634609979,
1.239916013, 1.944329746, 3.2536747, 3.732819078, 6.711769315,
2.307997621)), .Names = c("species", "area", "observp", "time"
), row.names = c(NA, 100L), class = "data.frame")
嗨,你可以提供'dput(dt)'的結果,或者它的一個子集,如果它太大,就像'dput(head(dt,100))'?另外,您嘗試過的第二個「聚合」是什麼? – Frank
Bernd,請使用'dput(yourobject)'或'dput(head(yourobject))'將您的數據(或假數據)樣本添加到您的問題中。 [這篇文章](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)是如何做到這一點的規範資源。 – SlowLearner
我不是一段時間(帳戶混淆)。標記的解決方案爲我工作。沒有時間檢查其他人。感謝您的親切幫助! –