【问题标题】:number of rows of result is not a multiple of vector length (arg 2) in R结果的行数不是R中向量长度(arg 2)的倍数
【发布时间】:2018-11-08 02:24:34
【问题描述】:

我有与我的主题相关的新问题 deleting outlier in r with account of nominal var。 在新情况下,变量 x 和 x1 具有不同的长度

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 和 x1。 我该如何决定这个问题? 这个解决方案对我没有帮助 Rsolnp: In cbind(temp, funv) : number of rows of result is not a multiple of vector length (arg 1) 或者我只是做错了什么。

编辑

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

错了 警告信息:

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 列如何得到这个输出

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)是多余的

【问题讨论】:

  • 好吧,你不能列绑定3个不同长度的向量,我不明白你的目标是什么。

标签: r dataframe dplyr mean outliers


【解决方案1】:

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

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 之前清理 xx1

编辑: 要获得所需的输出,请尝试以下代码(在 cmets 中添加 /ode 行):

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

【讨论】:

  • 我该如何决定这两个问题?
  • 第一个和你的数据逻辑有关,如果你了解你的数据和你的问题,你已经有了答案。第二个问题很简单,在我的回答中删除带有注释“#remove this”的代码行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-09
  • 1970-01-01
  • 1970-01-01
  • 2014-04-01
  • 2012-08-03
  • 1970-01-01
  • 2018-02-11
相关资源
最近更新 更多