Removing matrix rows if values of a cloumn are outliers

空扰寡人 提交于 2019-12-24 19:27:59

问题


There is a really cool and easy function by @aL3xa here but that is for a vector.

I have a matrix, and say column 2, is a variable that I want to chop off outliers and the associated row. There is a package outliers that I would like to use its algorithms, but they seem to be for a vector too. Any suggestions?

thanks


回答1:


Taking from some of the code from the question you linked:

# @aL3xa's function
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)
  y <- x
  y[x < (qnt[1] - H)] <- NA
  y[x > (qnt[2] + H)] <- NA
  y
}

set.seed(1)
x <- as.data.frame(matrix(rnorm(10000),ncol=100))  # 100 x 100 data frame
y <- remove_outliers(x[,2]) # look for outliers in columns 2

newx<-cbind(x,y)

newx2<-x[!is.na(x$y),] 


来源:https://stackoverflow.com/questions/20159275/removing-matrix-rows-if-values-of-a-cloumn-are-outliers

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!