R:如何聚合数据但保留非聚合列的信息?

时间:2013-10-07 21:56:06

标签: r aggregation

到目前为止搜索了两天的解决方案无济于事。

我从不同的观察点观察鸟类。观察者写下物种,他们在哪里看到它们,以及持续多久。

现在碰巧从不同的点开始,观察来自同一地区,但我们只想处理一个地区每个物种的最大值。

首先,我按观察点,物种和面积汇总数据,并总结时间。

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(丢弃)

当我随着时间和最大值对物种和面积进行另一次聚合时,观察点的信息就会丢失。

有人可以告诉我一个实现这个目标的方法吗?

干杯

Bernd

(现在有一个新帐户,没有声誉..谢谢...谷歌!)

P.S。请随意给这个问题一个更好的标题

更新: 尝试发布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")

2 个答案:

答案 0 :(得分:2)

您还可以查看另一个base函数by。输出是一个列表,其中每个元素是INDICES的不同组合的结果。

bb <- by(data = df, INDICES = list(df$species, df$area), function(x) x[which.max(x$time), ])
bb
# : Rm
# : A1
# observp species area     time
# 2      1c      Rm   A1 296.1527
# -------------------------------------------------------------------- 
# : Swm
# : A1
# observp species area     time
# 4      1a     Swm   A1 34.12771

如果您想将列表转换为data.frame

df2 <- do.call(rbind, bb)
df2
# observp species area      time
# 2      1c      Rm   A1 296.15271
# 4      1a     Swm   A1  34.12771

另一种选择:

library(plyr)
ddply(.data = df, .variables = .(species, area), subset,
  time == max(time))

答案 1 :(得分:0)

一个例子是

for (int k = 0; k <arraysize; k++) { list.add(getlist().get(k)); if(k == array.size()-1){ list.add(""); } }

stulevel_agg_2 <- stulevel[, list(a1=mean(ability, na.rm = TRUE), a2=last(school, na.rm=T)),by = grade]是新的列名。 a1, a2可以获取组中的最后一个元素,但您需要先加载last