如果cloumn的值离群,则删除矩阵行 [英] Removing matrix rows if values of a cloumn are outliers

查看:101
本文介绍了如果cloumn的值离群,则删除矩阵行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@ aL3xa在这里但这是一个向量.

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

我有一个矩阵,并且说第2列是一个我想剔除异常值和相关行的变量.我要使用一个软件包异常算法,但它们似乎也适用于向量.有什么建议吗?

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?

谢谢

推荐答案

从您链接的问题中获取一些代码:

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),] 

这篇关于如果cloumn的值离群,则删除矩阵行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆