gpt4 book ai didi

r - 满足条件时跟踪最后一次观察的组特定变量

转载 作者:行者123 更新时间:2023-12-02 08:00:09 25 4
gpt4 key购买 nike

我正在尝试构建一个特定于组的变量:

  1. 在满足条件之前进行观察。
  2. 满足条件时的第一次观察不适用。
  3. 对于后续观察,为满足条件时上次观察的值。

下面是 MWE。我正在寻找构建“last_value”的代码。我更喜欢使用 data.table(或 dplyr)来实现可扩展性/性能,但我愿意接受其他建议。谢谢!

library(data.table)

data.table(
group = c(rep("alpha", 4), rep("beta", 3)),
time = c(1:4, 1:3),
condition = c("Yes", "No", "Yes", "No", "No", "Yes", "No"),
value = 1:7,
last_value = c(NA, 1, 1, 3, NA, NA, 6))

# group time condition value last_value
# 1: alpha 1 Yes 1 NA
# 2: alpha 2 No 2 1
# 3: alpha 3 Yes 3 1
# 4: alpha 4 No 4 3
# 5: beta 1 No 5 NA
# 6: beta 2 Yes 6 NA
# 7: beta 3 No 7 6

# last_value is:

# NA in the 1st row as that is the 1st observation for group "alpha"
# 1 in the 2nd row as the 1st observation is condition = "Yes"
# 1 in the 3rd row as the 1st observation is condition = "Yes"; 2nd observation is condition = "No"
# 3 in the 4rd row as the 3rd observation is condition = "Yes"

# NA in the 5th row as that is the 1st observation for group "beta"
# NA in the 6th row as there is no prior observation with condition = "Yes"
# 6 in the 7th row as the 6th observation is condition = "Yes"

最佳答案

这是 dplyr 中使用 cummax 的另一个。

library(dplyr)
df %>%
group_by(group) %>%
mutate(last = cummax(row_number() * (condition == "Yes")),
last = lag(value[replace(last, last == 0, NA)]))


# group time condition value last_value last
# <fct> <int> <fct> <int> <dbl> <int>
#1 alpha 1 Yes 1 NA NA
#2 alpha 2 No 2 1 1
#3 alpha 3 Yes 3 1 1
#4 alpha 4 No 4 3 3
#5 beta 1 No 5 NA NA
#6 beta 2 Yes 6 NA NA
#7 beta 3 No 7 6 6

关于r - 满足条件时跟踪最后一次观察的组特定变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58334904/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com