2016-01-05 136 views
-2

我有在工作空間中rr兩個元件和ll(兩個矩陣)矩陣警告中的R

Warning message: 
In cbind(ll, rr) : 
    number of rows of result is not a multiple of vector length (arg 2) 
dim(ll) 
# [1] 3008 11 
length(rr) 
#[1] 3008 

如何可能?

+4

什麼是'dim(rr)'?你可以讓你的例子可重現嗎? – Heroka

+1

也許你的'rr'是一個向量(不是矩陣),所以它是水平的。嘗試轉置它。 – Marta

+2

@Marta WiIl使用**向量**(R中沒有方向)。不與1x3008 **矩陣**。 –

回答

6

爲了說明瑪爾塔的評論:

set.seed(42) 
ll <- matrix(rnorm(30),6, 5) 
rr <- matrix(rnorm(6), 1, 6) 
dim(ll) 
# [1] 6 5 
length(rr) 
# [1] 6 
cbind(ll, rr) 
Error in cbind(ll, rr) : 
    number of rows of matrices must match (see arg 2) 

cbind(ll, t(rr)) 
#   [,1]  [,2]  [,3]  [,4]  [,5]  [,6] 
# [1,] 1.3709584 1.51152200 -1.3888607 -2.4404669 1.8951935 0.4554501 
# [2,] -0.5646982 -0.09465904 -0.2787888 1.3201133 -0.4304691 0.7048373 
# [3,] 0.3631284 2.01842371 -0.1333213 -0.3066386 -0.2572694 1.0351035 
# [4,] 0.6328626 -0.06271410 0.6359504 -1.7813084 -1.7631631 -0.6089264 
# [5,] 0.4042683 1.30486965 -0.2842529 -0.1719174 0.4600974 0.5049551 
# [6,] -0.1061245 2.28664539 -2.6564554 1.2146747 -0.6399949 0.4554501 

爲了說明Heroka的評論:

dim(ll) 
# [1] 6 5 
dim(rr) 
# [1] 1 6 

如果ll是矩陣和rr一個矢量(R感),即

rr <- as.numeric(rr) 
dim(rr) 
# NULL 
length(rr) 
# 6 
cbind(ll, rr) 
#                  rr 
# [1,] 1.3709584 1.51152200 -1.3888607 -2.4404669 1.8951935 0.4554501 
# [2,] -0.5646982 -0.09465904 -0.2787888 1.3201133 -0.4304691 0.7048373 
# [3,] 0.3631284 2.01842371 -0.1333213 -0.3066386 -0.2572694 1.0351035 
# [4,] 0.6328626 -0.06271410 0.6359504 -1.7813084 -1.7631631 -0.6089264 
# [5,] 0.4042683 1.30486965 -0.2842529 -0.1719174 0.4600974 0.5049551 
# [6,] -0.1061245 2.28664539 -2.6564554 1.2146747 -0.6399949 0.4554501 

要擺脫列名稱:

cbind(ll, unname(rr))