[英]Filter groups only by increasing, ascending order in R
我无法弄清楚如何过滤 dataframe 中按降序增加的组。 我将从一个输入开始:
df <- structure(list(Animal = c("Cat", "Cat", "Cat", "Cat", "Cat",
"Cat", "Dog", "Dog", "Dog", "Dog", "Rat", "Rat", "Rat", "Rat",
"Rat"), Value = c(2L, 3L, 5L, 8L, 19L, 20L, 4L, 8L, 19L, 33L,
4L, 8L, 15L, 10L, 25L)), class = "data.frame", row.names = c(NA,
-15L))
在这种情况下,我希望我的 output 只显示组“狗”和“猫”,因为它一直在增加。 然而,当它从 8 到 15 再到 10 时,老鼠会被排除在外。
任何帮助,将不胜感激。 谢谢!
一种基于dplyr
的解决方案可能是:
df %>%
group_by(Animal) %>%
filter(all(diff(Value) > 0))
Animal Value
<chr> <int>
1 Cat 2
2 Cat 3
3 Cat 5
4 Cat 8
5 Cat 19
6 Cat 20
7 Dog 4
8 Dog 8
9 Dog 19
10 Dog 33
数据:
df <- structure(list(Animal = c("Cat", "Cat", "Cat", "Cat", "Cat",
"Cat", "Dog", "Dog", "Dog", "Dog", "Rat", "Rat", "Rat", "Rat",
"Rat"), Value = c(2L, 3L, 5L, 8L, 19L, 20L, 4L, 8L, 19L, 33L,
4L, 8L, 15L, 10L, 25L)), class = "data.frame", row.names = c(NA,
-15L))
代码:
library(dplyr)
df %>%
filter(!Animal == "Rat") %>%
arrange(Animal, Value)
Output:
#> Animal Value
#> 1 Cat 2
#> 2 Cat 3
#> 3 Cat 5
#> 4 Cat 8
#> 5 Cat 19
#> 6 Cat 20
#> 7 Dog 4
#> 8 Dog 8
#> 9 Dog 19
#> 10 Dog 33
由reprex package (v0.3.0) 创建于 2020-08-18
我会定义一个 function 称为increasing
based on diff
,它返回 boolean。以下是dplyr
和data.table
的版本:
library(dplyr)
library(data.table)
df <- tibble::tribble(
~Animal, ~Value,
"Cat", 2,
"Cat", 3,
"Cat", 5,
"Cat", 8,
"Cat", 19,
"Cat", 20,
"Dog", 4,
"Dog", 8,
"Dog", 19,
"Dog", 33,
"Rat", 4,
"Rat", 8,
"Rat", 15,
"Rat", 10,
"Rat", 25)
dt <- data.table(df)
increasing <- function(x) rep(all(diff(x) >= 0), length(x))
df %>% group_by(Animal) %>%
filter(increasing(Value))
dt[, .SD[increasing(Value)], by=Animal]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.