[英]Change cell value by comparing to cell above
I have the following dataframe:我有以下数据框:
ID year level
1 2000 NA
1 2001 3
1 2002 3
1 2003 2
1 2004 1
2 2000 1
2 2001 3
2 2002 3
2 2003 3
2 2004 3
I want to update each value in "level" column by ID based on the previous one if the previous one is smaller.如果前一个较小,我想根据前一个 ID 按 ID 更新“级别”列中的每个值。
the dataframe should look like this数据框应如下所示
ID year level
1 2000 NA
1 2001 3
1 2002 3
1 2003 2
1 2004 1
2 2000 1
2 2001 1
2 2002 1
2 2003 1
2 2004 1
I tried using shift from data table but it only changes one cell.我尝试使用数据表中的移位,但它只更改一个单元格。 I got this result我得到了这个结果
ID year level
1 2000 NA
1 2001 3
1 2002 3
1 2003 2
1 2004 1
2 2000 1
2 2001 1
2 2002 3
2 2003 3
2 2004 3
library(data.table)
df <- data.frame(
ID = c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L),
year = c(2000L,2001L,2002L,2003L,2004L,
2000L,2001L,2002L,2003L,2004L),
level = c(NA, 3L, 3L, 2L, 1L, 1L, 1L, 1L, 1L, 1L)
)
setDT(df)[!is.na(level), level := cummin(level), by = ID][]
#> ID year level
#> 1: 1 2000 NA
#> 2: 1 2001 3
#> 3: 1 2002 3
#> 4: 1 2003 2
#> 5: 1 2004 1
#> 6: 2 2000 1
#> 7: 2 2001 1
#> 8: 2 2002 1
#> 9: 2 2003 1
#> 10: 2 2004 1
Created on 2022-07-06 by the reprex package (v2.0.1)由reprex 包(v2.0.1) 创建于 2022-07-06
A tidyverse
solution with accumulate()
from purrr
:来自purrr
的带有accumulate()
的tidyverse
解决方案:
library(tidyverse)
df %>%
group_by(ID) %>%
mutate(level2 = accumulate(level, min, na.rm = TRUE)) %>%
ungroup()
# # A tibble: 10 × 4
# ID year level level2
# <int> <int> <int> <int>
# 1 1 2000 NA NA
# 2 1 2001 3 3
# 3 1 2002 3 3
# 4 1 2003 2 2
# 5 1 2004 1 1
# 6 2 2000 1 1
# 7 2 2001 3 1
# 8 2 2002 3 1
# 9 2 2003 3 1
# 10 2 2004 3 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.