考虑名义var删除r中的离群值 [英] deleting outlier in r with account of nominal var

查看:79
本文介绍了考虑名义var删除r中的离群值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说,我有三列

x <- c(-10, 1:6, 50)
x1<- c(-20, 1:6, 60)
z<- c(1,2,3,4,5,6,7,8)

检查x的异常值

bx <- boxplot(x)
bx$out

检查x1的异常值

bx1 <- boxplot(x1)
bx1$out

现在我们必须删除异常值

now we must delete outliers

x <- x[!(x %in% bx$out)]
x

x1 <- x1[!(x1 %in% bx1$out)]
x1

,但是我们有变量Z(标称值),我们必须删除与变量x和x1的异常值相对应的观测值, 在我们的例子中是1和8磅. Z的

but we have variable Z(nominal) and we must remove observations, which correspond to the outlier of variables x and x1, in our case it is 1 and 8 obs. of Z

该怎么做? 在输出中我们必须有

How to do it? in output we must have

x   x1  z
Na  Na  Na
1   1   2
2   2   3
3   3   4
4   4   5
5   5   6
6   6   7
Na  Na  Na

推荐答案

尝试以下解决方案:

x_to_remove<-which(x %in% bx$out)
x <- x[!(x %in% bx$out)]

x1_to_remove<-which(x1 %in% bx1$out)
x1 <- x1[!(x1 %in% bx1$out)]

z<-z[-unique(c(x_to_remove,x1_to_remove))]
z    
[1] 2 3 4 5 6 7

在删除xx1中的值之前,您必须保存位置(x_to_removex1_to_remove),然后用于清理z.

Before delete values in x and x1 you have to save the positions (x_to_remove and x1_to_remove) and than use to clean z.

您的输出:

data.frame(cbind(x,x1,z))
  x x1 z
1 1  1 2
2 2  2 3
3 3  3 4
4 4  4 5
5 5  5 6
6 6  6 7

这篇关于考虑名义var删除r中的离群值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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