结果的行数不是R中向量长度(arg 2)的倍数 [英] number of rows of result is not a multiple of vector length (arg 2) in R

查看:1487
本文介绍了结果的行数不是R中向量长度(arg 2)的倍数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有与此主题相关的新问题 根据名义var删除r中的异常值. 在新情况下,变量x和x1的长度不同

I have new question related with this my topic deleting outlier in r with account of nominal var. In new case variables x and x1 has different lenght

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

bx <- boxplot(x)
bx$out

bx1 <- boxplot(x1)
bx1$out


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


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  

data.frame(cbind(x,x1,z))

然后我得到警告

Warning message:
In cbind(x, x1, z) :
  number of rows of result is not a multiple of vector length (arg 2)

所以在新的数据框中,obs. Z的x不对应于x和x1. 我该如何解决这个问题? 这种解决办法对我没有帮助 Rsolnp:在cbind(temp,funv)中:结果的行数不是向量长度(arg 1)的倍数 还是我做错了什么.

so in new dataframe the obs. of Z is not corresponding to x and x1. How can i decide this problem? This solustion is not help me Rsolnp: In cbind(temp, funv) : number of rows of result is not a multiple of vector length (arg 1) or i just do anything wrong.

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  

d=data.frame(cbind(x,x1,z))
d

这是错误的 警告消息:

it is wrong Warning message:

In cbind(x, x1, z) :
  number of rows of result is not a multiple of vector length (arg 2)

d

  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  1 2

在这3个列上如何获得此输出

How on this 3 columg get this output

Na  Na  Na
1   1   2
2   2   3
3   3   4
4   4   5
5   5   6
Na  Na  Na
Na  Na  Na

六行(d)是多余的

推荐答案

原始x,x1和z列表中的长度不同是第一个问题,您怎么说哪个z值与每个x和x1值相关? >

Differents lengths in original x, x1 and z lists is the first problem, how can you say which z values is related to each x and x1 values?

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

另一个问题在这里:

x<- x[!(x %in% bx$out)] #remove this
x1 <- x1[!(x1 %in% bx1$out)] #remove this


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

在计算x_to_removex1_to_remove

要获得所需的输出,请尝试以下代码(/ode行已添加注释中的签名):

To achieve your desired output try this code (/ode lines added signed in comments):

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

length_max<-min(length(x),length(x1),length(z)) #Added: identify max length before outlier detection

bx <- boxplot(x)
bx1 <- boxplot(x1)

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

length_min<-min(length(x),length(x1),length(z)) #Minimum length after outlier remove

d=data.frame(cbind(x[1:length_min],x1[1:length_min],z[1:length_min])) #Bind columns
colnames(d)<-c("x","x1","z")

d_NA<-as.data.frame(matrix(rep(NA,(length_max-length_min)*3),nrow=(length_max-length_min))) #Create NA rows
 colnames(d_NA)<-c("x","x1","z")

d<-rbind(d,d_NA) #Your desired output
d
   x x1  z
1  1  1  2
2  2  2  3
3  3  3  4
4  4  4  5
5  5  5  6
6 NA NA NA
7 NA NA NA

这篇关于结果的行数不是R中向量长度(arg 2)的倍数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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