【发布时间】:2016-02-22 00:56:00
【问题描述】:
我的问题来自How to find tail rows of a data frame that satisfy set criteria?,因此,我的(更新的)样本数据的结构如下:
Individ <- data.frame(Participant = c("Bill", "Bill", "Bill", "Bill", "Bill", "Jane", "Jane", "Jane", "Jane", "Jane", "Jane", "Jane", "Jane", "Jane", "Jane", "Jane", "Jane", "Bill", "Bill", "Bill", "Bill"),
Time = c(1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4),
Condition = c("Placebo", "Placebo", "Placebo", "Placebo", "Placebo", "Expr", "Expr", "Expr", "Expr", "Expr", "Expr", "Placebo", "Placebo", "Placebo", "Placebo", "Placebo", "Placebo", "Expr", "Expr", "Expr", "Expr"),
Location = c("Home", "Home", "Home", "Home", "Home", "Home", "Home", "Home", "Home", "Home", "Home", "Home", "Home", "Home", "Home", "Home", "Home", "Away", "Away", "Away", "Away"),
Power = c(400, 250, 180, 500, 300, 600, 512, 300, 500, 450, 200, 402, 210, 130, 520, 310, 451, 608, 582, 390, 570))
我已经学会根据Power 的最后一次出现在不同的Condition 加上Location 中找到每个Participant 的尾行。我现在希望为每个Condition 和Location 从每个Participant 中删除最后3 行。但是,为每个Participant 和Condition 收集的Time 不同,因此我不能纯粹基于标准化的Time 删除行。
如何快速遍历每个 Participant 和它们各自的 Conditionplus Location 并删除最后 3 行?我的实际数据框是 400 万行 + 超过 50 名参与者,因此理想情况下,需要迭代每个 Participant 和 Condition 的解决方案。
我的预期输出是:
Output <- data.frame(Participant = c("Bill", "Bill", "Jane", "Jane", "Jane", "Jane", "Jane", "Jane", "Bill"),
Time = c(1, 2, 1, 2, 3, 1, 2, 3, 1),
Condition = c("Placebo", "Placebo", "Expr", "Expr", "Expr", "Placebo", "Placebo", "Placebo", "Expr"),
Location = c("Home", "Home", "Home", "Home", "Home", "Home", "Home", "Home", "Away"),
Power = c(400, 250, 600, 512, 300, 402, 210, 130, 608))
【问题讨论】:
标签: r