将向量转换为矩阵(在R中)

时间:2018-05-25 14:03:32

标签: r matrix reshape spread dcast

我想从“n x 1”向量

创建一个“n x 8”矩阵

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

- 答案:为了矩阵乘以“8×8”马尔可夫链概率转移矩阵,并返回预测状态的“n×8”矩阵

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

R代码

创建一个虚拟的“n x 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"

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

尝试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():

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

我试过spread():

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"

1 个答案:

答案 0 :(得分: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"