如何将功能应用于特定列

时间:2020-08-01 22:26:11

标签: r function dataframe apply outliers

我有一个如下所示的示例数据框:

well <- c('A1','A2','A3','A4','A5')
area <- c(21000, 23400, 26800,70000,8000)
length <- c(21, 234, 26,70,22)
group<-c('WT','Control','C2','D2','E1')

data <- data.frame(well,area,length,group)

我想应用以下函数从数据框中删除带有异常值的行:

Outlier <- function(x){
  low <- median(x, na.rm=TRUE)-5*(mad(x)) 
  high <- median(x, na.rm=TRUE)+5*(mad(x))   
  out <- if_else(x > high, NA,ifelse(x < low, low, x)) 
  out }

如何将此功能应用于除某些列(例如“ well”和“ group”列)以外的数据框?

1 个答案:

答案 0 :(得分:2)

我们可以在lapply中使用base R

data[c('area', 'length')] <- lapply(data[c('area', 'length')], Outlier)

或与dplyr

library(dplyr) # 1.0.0
data %>% 
     mutate(across(area:length, Outlier))
#    well  area length   group
#1   A1 21000     21      WT
#2   A2 23400     NA Control
#3   A3 26800     26      C2
#4   A4    NA     NA      D2
#5   A5  8000     22      E1

注意:请确保在“异常值”功能中将NA更改为NA_real_

Outlier <- function(x){
  low <- median(x, na.rm=TRUE)-5*(mad(x)) 
  high <- median(x, na.rm=TRUE)+5*(mad(x))   
  out <- if_else(x > high, NA_real_,ifelse(x < low, low, x)) 
  out }