gpt4 book ai didi

r - 如何过滤数据帧以获取连续增加的值

转载 作者:行者123 更新时间:2023-12-03 19:06:42 24 4
gpt4 key购买 nike

我有一个包含多列的大型数据框,但对于此查询,我对 3 列感兴趣。

df <- structure(list(country = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "ireland", class = "factor"), 
parameter = structure(c(2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L,2L, 1L, 3L), .Label = c("rainfall", "temp", "wind"), class = "factor"),
value = c(10L, 15L, 20L, 9L, 18L, 10L, 12L, 25L, 15L, 10L, 10L, 20L), unit = structure(c(3L, 2L, 1L, 3L, 2L, 1L, 3L,
2L, 1L, 3L, 2L, 1L), .Label = c("km/hr", "mm", "oC"), class = "factor")), class = "data.frame", row.names = c(NA, -12L))

country parameter value unit
ireland temp 10 oC
ireland rainfall 15 mm
ireland wind 20 km/hr
ireland temp 9 oC
ireland rainfall 18 mm
ireland wind 10 km/hr
ireland temp 12 oC
ireland rainfall 25 mm
ireland wind 15 km/hr
ireland temp 10 oC
ireland rainfall 10 mm
ireland wind 20 km/hr
我想按国家和参数分组以提取值列连续增加 3 次或更多次的行。
欲望输出的例子。
country parameter   value   unit
ireland rainfall 15 mm
ireland rainfall 18 mm
ireland rainfall 25 mm
ireland wind 10 km/hr
ireland wind 15 km/hr
ireland wind 20 km/hr

最佳答案

您可以按国家和参数分组,它们为不小于滞后值的值创建第三个分组变量,然后过滤组大小为 3 或更大:

library(dplyr)

df %>%
group_by(country, parameter) %>%
group_by(x = cumsum(value <= lag(value, default = FALSE)), .add = TRUE) %>%
filter(n() >= 3) %>%
ungroup() %>%
arrange(country, parameter) %>%
select(-x)

# A tibble: 6 x 4
country parameter value unit
<fct> <fct> <int> <fct>
1 ireland rainfall 15 mm
2 ireland rainfall 18 mm
3 ireland rainfall 25 mm
4 ireland wind 10 km/hr
5 ireland wind 15 km/hr
6 ireland wind 20 km/hr

关于r - 如何过滤数据帧以获取连续增加的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63394724/

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