假設我有一個由傳輸模型生成的案例數量的時間序列,每個獨特的模型分別標記
trial.df <- data.frame(
model = rep(1:3, each =5),
time = rep(1:5, 3),
cases= rnorm(15, mean = 1, sd = 0.2)
)
在現實世界中,一旦達到 < 1 個病例,該疾病應該消失,但由于模型代碼的作業方式,即使病例低于 1,您仍然可以有大于 1 的病例。我如何強制所有值在值 < 1 之后為零?
我嘗試過濾每個模型出現零的第一個時間點,然后用零替換該時間點之后出現的所有值,但它不適用于我當前的代碼
trial.df %>%
group_by(model) %>%
filter(floor(cases) == 0) %>%
slice(1) %>%
ungroup() -> rows
for (i in nrow(rows)){
trial.df %>%
group_by(model) %>%
mutate(cases2= case_when(
model == rows$model[i] & time >= rows$time[i] ~ 0,
TRUE ~ cases
)) %>% print(n = Inf)
}
我也嘗試過replace()這樣做,但這不考慮新的替換值,只考慮原始資料中的值
trial.df %>%
group_by(model) %>%
mutate(cases2 = replace(cases, floor(lag(cases)) == 0, 0))
我覺得這應該相對簡單,但似乎無法讓它發揮作用。非常感謝您的建議!謝謝
uj5u.com熱心網友回復:
您可以使用dplyr::cumany:
set.seed(42)
trial.df <- data.frame(
model = rep(1:3, each =5),
time = rep(1:5, 3),
cases= rnorm(15, mean = 1, sd = 0.2)
)
trial.df %>%
group_by(model) %>%
mutate(cases2 = if_else(cumany(cases < 1), 0, cases)) %>%
ungroup()
# # A tibble: 15 x 4
# model time cases cases2
# <int> <int> <dbl> <dbl>
# 1 1 1 1.27 1.27
# 2 1 2 0.887 0
# 3 1 3 1.07 0
# 4 1 4 1.13 0
# 5 1 5 1.08 0
# 6 2 1 0.979 0
# 7 2 2 1.30 0
# 8 2 3 0.981 0
# 9 2 4 1.40 0
# 10 2 5 0.987 0
# 11 3 1 1.26 1.26
# 12 3 2 1.46 1.46
# 13 3 3 0.722 0
# 14 3 4 0.944 0
# 15 3 5 0.973 0
uj5u.com熱心網友回復:
感謝 Henrik 的回應,它作業得非常出色!不知道為什么我現在看不到你的答案,所以分享它以便其他人可以學習。
使用cummin()功能
x = c(2, 1, 0.5, 0, 1); x * cummin(x >= 1)
trial.df %>%
group_by(model) %>%
mutate(cases2= cases*cummin(cases>=1)) %>% print(n = Inf)
我確信有一個簡單的解決方案。我真的應該更了解cum..()函式的使用!再次感謝!
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/343000.html
標籤:r
