简体   繁体   English

数据框中的异常值,但我想对 R 中的数据框中的分组行执行此操作

[英]Outlier in dataframe, but I want to do it for grouped rows in a dataframe in R

Example dataframe.示例数据框。 在此处输入图片说明

I want to detect outliers per group and display it in a separate dataframe, for example, for each species name, anthopleura aureradiata , I want to look at values 27.75, 6.83, and 23.91, and calculate the outliers between these values.我想检测每组的异常值并将其显示在单独的数据框中,例如,对于每个物种名称anthopleura aureradiata ,我想查看值27.75、6.83和 23.91,并计算这些值之间的异常值。 If I find that row 4 is an outlier for that particular species, I want to display it in my new dataframe.如果我发现第 4 行是该特定物种的异常值,我想在我的新数据框中显示它。 Does anyone know how to get about this?有谁知道如何解决这个问题?

Reproducible example:可重现的例子:

x = data.frame("species" = c("Agao", "Beta", "Beta", "Beta", "Carrot", "Carrot"), "sum" = c(1, 100, 5, 4, 3, 0))

We can modify this function based on our requirement and use it to filter outliers for each group and create a new dataframe.我们可以根据我们的要求修改这个函数,并使用它来过滤每个组的异常值并创建一个新的数据框。

library(dplyr)

remove_outliers <- function(x, na.rm = TRUE, ...) {
    qnt <- quantile(x, probs=c(.25, .75), na.rm = na.rm, ...)
    H <- 1.5 * IQR(x, na.rm = na.rm)
    x < (qnt[1] - H) | x > (qnt[2] + H)
}

separate_dataframe <- x %>% group_by(species) %>% filter(remove_outliers(sum))
separate_dataframe

# species   sum
#  <fct>   <dbl>
#1 Beta     -100

data数据

x = data.frame(species = c("Agao", "Beta", "Beta", "Beta", "Beta", 
              "Carrot", "Carrot"),sum = c(1, 1, 5, 4, -100, 3,0))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM