如何获取r中数据帧中所有列的异常值

如何获取r中数据帧中所有列的异常值,r,R,我正在研究一个通用函数,它将获取dataframe并返回dataframe中每个变量的所有异常值,然后将其删除 outliers <- function(dataframe){ dataframe <- select_if(dataframe, is.numeric) for(i in 1:length(dataframe)){ paste(names(dataframe)[i]) <- boxplot.stats(names(dataframe)[i])$

我正在研究一个通用函数,它将获取dataframe并返回dataframe中每个变量的所有异常值,然后将其删除

 outliers <- function(dataframe){
   dataframe <- select_if(dataframe, is.numeric)
   for(i in 1:length(dataframe)){
   paste(names(dataframe)[i]) <- boxplot.stats(names(dataframe)[i])$out)

  }
}

您可以从
Clean\u data=read.csv('http://ucanalytics.com/blogs/wp-content/uploads/2016/09/Regression-Clean-Data.csv“)

我们通过只选择
数值列(
select\u if
)创建一个函数,循环遍历这些列(
map
),并将非异常值的元素子集。这将输出为
向量的
列表

library(dplyr)
library(tidyr)
library(purrr)
outlierremoval <- function(dataframe){
 dataframe %>%
      select_if(is.numeric) %>% #selects on the numeric columns
      map(~ .x[!.x %in% boxplot.stats(.)$out]) #%>%
      # not clear whether we need to output as a list or data.frame
      # if it is the latter, the columns could be of different length
      # so we may use cbind.fill
      # { do.call(rowr::cbind.fill, c(., list(fill = NA)))}

 }

outlierremoval(Clean_Data)
更新 如果我们需要获取异常值,在
map
步骤中,我们从
boxplot.stats

outliers <- function(dataframe){
dataframe %>%
     select_if(is.numeric) %>% 
      map(~ boxplot.stats(.x)$out) 
  

  }
outliers(Clean_Data)
这是我对数据所做的

df%
地图(~boxplot.stats(.x)$out)
}

异常值那么问题是什么?我的函数不起作用。我想输出不同向量中所有变量的所有异常值。你能提供一个可重复的例子吗?数据,使用的包,期望的输出…我已经添加了数据。在输出中,我希望所有数值变量及其各自的异常值在一个单独的向量中,谢谢您的回答。我们还可以输出不同向量中的所有异常值吗?@Neil我们可以将其输出为
向量的
列表。更新post@Neil顺便说一句,当你说要删除异常值时,是要删除该观察值还是将其更改为NA?我可以使用
complete.cases
删除它。只有一个问题,
map
函数做什么?非常感谢您的回答和解释。非常感谢!
outlierremoval <- function(dataframe){
 dataframe %>%          
       map_if(is.numeric, ~ .x[!.x %in% boxplot.stats(.)$out]) %>%
       { do.call(rowr::cbind.fill, c(., list(fill = NA)))} %>%
       set_names(names(dataframe))
     


}
res <- outlierremoval(Clean_Data)
head(res)
#  X Observation Dist_Taxi Dist_Market Dist_Hospital Carpet Builtup      Parking City_Category Rainfall House_Price
#1 1           1      9796        5250         10703   1659    1961         Open         CAT B      530     6649000
#2 2           2      8294        8186         12694   1461    1752 Not Provided         CAT B      210     3982000
#3 3           3     11001       14399         16991   1340    1609 Not Provided         CAT A      720     5401000
#4 4           4      8301       11188         12289   1451    1748      Covered         CAT B      620     5373000
#5 5           5     10510       12629         13921   1770    2111 Not Provided         CAT B      450     4662000
#6 6           6      6665        5142          9972   1442    1733         Open         CAT B      760     4526000
outliers <- function(dataframe){
dataframe %>%
     select_if(is.numeric) %>% 
      map(~ boxplot.stats(.x)$out) 
  

  }
outliers(Clean_Data)
outlierreplacement <- function(dataframe){
   dataframe %>%          
           map_if(is.numeric, ~ replace(.x, .x %in% boxplot.stats(.x)$out, NA)) %>%
           bind_cols 
         

  
}
outlierreplacement(Clean_Data)
df <- as.data.frame(read.csv("heart.csv"))
boxplot(df)
findOutliers <- function(dataframe){
  dataframe %>%Heart Disease UCI
    select_if(is.numeric) %>% 
    map(~ boxplot.stats(.x)$out)
}
outliers <- findOutliers(df)
temp <- list()
for (col in names(outliers)) {
  outlier <- outliers[[col]]
  if (length(outlier) > 0) {
    temp[col] <- df[-which(df[[col]] %in% outlier),][col]
  } else {
    temp[col] <- df[col]
  }
}
boxplot(temp)