2

我想从“nx 1”向量创建一个“nx 8”矩阵

——问题:我为什么要这样做?

-- 答案:为了将它与“8 x 8”马尔可夫链概率转移矩阵相乘,并返回预测状态的“nx 8”矩阵

--解决方案:我已经在下面的尝试 3 中解决了这个问题 - 但想知道是否有更好的方法来解决这个问题(而不是使用两个转置函数)?


R代码

创建一个虚拟的“nx 1”向量:(这里我们使用 n = 2)

> temp_vector <- c("state 4", "state 7")
> temp_vector
[1] "state 4" "state 7"

预期输出:

NA NA NA TRUE NA NA NA NA
NA NA NA NA NA NA TRUE NA

尝试1:转换为矩阵:

> temp_matrix <- matrix(temp_vector, 
                ncol = 8, # there are 8 states
                nrow = length(temp_vector) # there are 10 rows in the vector
                )
> temp_matrix
     [,1]      [,2]      [,3]      [,4]      [,5]      [,6]      [,7]      [,8]     
[1,] "state 4" "state 4" "state 4" "state 4" "state 4" "state 4" "state 4" "state 4"
[2,] "state 7" "state 7" "state 7" "state 7" "state 7" "state 7" "state 7" "state 7"

尝试 1 失败:这并不理想,我想要一个矩阵,每行一个条目,而不是八个。


尝试 2:将上面的 stateSpace 与矩阵进行比较,得到一个由 TRUE/FALSE 组成的矩阵:

> stateSpace <- c("state 1", "state 2", "state 3", "state 4", "state 5", "state 6", "state 7", "state 8")

> temp_matrix == stateSpace
     state 1 state 2 state 3 state 4 state 5 state 6 state 7 state 8
[1,]   FALSE   FALSE   FALSE   FALSE   FALSE   FALSE   FALSE   FALSE
[2,]   FALSE   FALSE   FALSE   FALSE   FALSE   FALSE   FALSE   FALSE

尝试 2 失败:期望每一行都有一个 TRUE 和其余的 FALSE

原因:(我认为)矩阵是按列比较的。


进一步研究尝试 2,在逐个元素级别上这是有效的:

> temp_matrix[1,1] == colnames(temp_matrix)[1]
state 1 
  FALSE 
> temp_matrix[1,2] == colnames(temp_matrix)[2]
state 2 
  FALSE 
> temp_matrix[1,3] == colnames(temp_matrix)[3]
state 3 
  FALSE 
> temp_matrix[1,4] == colnames(temp_matrix)[4]
state 4 
   TRUE 

进一步研究尝试 2,在逐行级别上这是有效的:

> temp_matrix[1,] == colnames(temp_matrix)[]
state 1 state 2 state 3 state 4 state 5 state 6 state 7 state 8 
  FALSE   FALSE   FALSE    TRUE   FALSE   FALSE   FALSE   FALSE 

> temp_matrix[2,] == colnames(temp_matrix)[]
state 1 state 2 state 3 state 4 state 5 state 6 state 7 state 8 
  FALSE   FALSE   FALSE   FALSE   FALSE   FALSE    TRUE   FALSE 

尝试 3:在注意到上述 R 中按列比较的学习之后

> t(stateSpace == t(temp_matrix))
     state 1 state 2 state 3 state 4 state 5 state 6 state 7 state 8
[1,]    TRUE   FALSE   FALSE   FALSE   FALSE   FALSE   FALSE   FALSE
[2,]   FALSE    TRUE   FALSE   FALSE   FALSE   FALSE   FALSE   FALSE

尝试 3 成功:创建了这个 stackoverflow 帖子,看看是否有更好的方法来解决这个问题(而不是使用两个转置函数)


其他选项:dcast、reshape、spread;可悲的是也没有工作。

我试过重塑():

reshape(temp_vector, direction = "wide")
> Error in data[, timevar] : incorrect number of dimensions

我试过传播():

library(tidyr)
spread(temp_vector, key = numbers, value = value)
> Error in UseMethod("spread_") : 
  no applicable method for 'spread_' applied to an object of class "factor"
4

1 回答 1

0

试试这个:

> v <- c("state 4", "state 7")
> states <- c("state 1", "state 2", "state 3", "state 4",
+             "state 5", "state 6", "state 7", "state 8")
> m <- matrix(states, byrow = TRUE, nrow = 2, ncol = 8)
> m
#      [,1]      [,2]      [,3]      [,4]      [,5]      [,6]      [,7]      # [,8]     
# [1,] "state 1" "state 2" "state 3" "state 4" "state 5" "state 6" "state 7" "state 8"
# [2,] "state 1" "state 2" "state 3" "state 4" "state 5" "state 6" "state 7" "state 8"
> v == m
#       [,1]  [,2]  [,3]  [,4]  [,5]  [,6]  [,7]  [,8]
# [1,] FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE
# [2,] FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE

在 R 中,矩阵基本上是引擎盖下的向量。当m在上面创建时,matrix函数“回收”它的参数spaces,因为它需要创建一个包含 16 个元素的矩阵。换句话说,以下两个函数调用产生相同的结果:

> matrix(states, byrow = TRUE, nrow = 2, ncol = 8)
> matrix(rep(states, 2), byrow = TRUE, nrow = 2, ncol = 8)

类似地,当vm进行相等比较时,v会循环 8 次以生成长度为 16 的向量。换句话说,以下两个相等比较产生相同的结果:

> v == m
> rep(v, 8) == m

您可以将上述两个比较视为发生在两个向量之间,其中矩阵m通过堆叠列转换回向量。您可以使用查看对应于as.vector的向量:m

> as.vector(m)
#  [1] "state 1" "state 1" "state 2" "state 2" "state 3" "state 3" "state 4" "state 4" "state 5"
# [10] "state 5" "state 6" "state 6" "state 7" "state 7" "state 8" "state 8"
于 2018-06-04T06:30:11.887 回答