【问题标题】:Converting array to matrix in R在R中将数组转换为矩阵
【发布时间】:2017-04-16 17:28:00
【问题描述】:

我有一个数组,包括一个名为“comp”的项目(是,否)上的两个熟练度变量(theta0,theta1)。这需要转换为一个矩阵。有什么方法可以转换底部的矩阵吗?

我的数组如下所示:

>priCPT.i6

 , , comp = Yes

 theta1
theta0       Low       Med      High
  Low  0.8377206 0.6760511 0.4576021
  Med  0.6760511 0.4576021 0.2543239
  High 0.4576021 0.2543239 0.1211734

, , comp = No

  theta1
theta0       Low       Med      High
  Low  0.1622794 0.3239489 0.5423979
  Med  0.3239489 0.5423979 0.7456761
  High 0.5423979 0.7456761 0.8788266

attr(,"class")
[1] "CPA"   "array"

抱歉,我无法制作出你可以玩的东西。我正在寻找类似的东西:

theta0   theta1   Yes        No
Low      Low      0.8377206  0.1622794
Low      Med      ..         ..
Low      High     ..         ..
Med      Low      ..         ..
Med      Med      ..         ..
Med      High     ..         ..
High     Low      ..         ..
High     Med      ..         ..
High     High     ..         ..

问候...

【问题讨论】:

  • tidyr::spread(as.data.frame.table(priCPT.i6), Var3, Freq)

标签: arrays r matrix data-conversion


【解决方案1】:

您可以通过展平第 3 个边距的矩阵来轻松获取值列:

z1 <- apply(priCPT.i6, 3L, c)
## we can also simply use `matrix`; but remember to set `dimnames`
## otherwise we lose dimnames
## z1 <- matrix(priCPT.i6, ncol = 2L,
##              dimnames = list(NULL, dimnames(priCPT.i6)[[3]]))

剩下的你需要添加“dimnames”列:

z2 <- expand.grid(dimnames(priCPT.i6)[1:2])

现在您可以将它们合并成一个数据框(您肯定需要一个数据框而不是矩阵,因为z1 的列是数字,而z2 的列是字符)通过:

data.frame(z2, z1)

可重现的示例

x <- array(1:18, dim = c(3L, 3L, 2L), dimnames = list(
           c("Low", "Medium", "High"), c("Low", "Medium", "High"), c("Yes", "No")))

#, , Yes
#
#       Low Medium High
#Low      1      4    7
#Medium   2      5    8
#High     3      6    9
#
#, , No
#
#       Low Medium High
#Low     10     13   16
#Medium  11     14   17
#High    12     15   18

z1 <- apply(x, 3L, c)
## z1 <- matrix(x, ncol = 2L, dimnames = list(NULL, dimnames(x)[[3]]))
z2 <- expand.grid(dimnames(x)[1:2])
data.frame(z2, z1)

#    Var1   Var2 Yes No
#1    Low    Low   1 10
#2 Medium    Low   2 11
#3   High    Low   3 12
#4    Low Medium   4 13
#5 Medium Medium   5 14
#6   High Medium   6 15
#7    Low   High   7 16
#8 Medium   High   8 17
#9   High   High   9 18

【讨论】:

  • 感谢您快速准确的回复。这完全回答了我的问题。我真的很感激。祝你有美好的一天!
  • 啊,我明白了。我最初想使用as.data.frame.table(x):但我找不到合适的应用方式,只有dcast(as.data.frame.table(x), Var1 + Var2 ~ Var3, value.var="Freq") 不会很快
  • ftable(x) 如果它返回一些有用的东西是最好的。或dd &lt;- data.frame(as.table(x)); reshape(dd, dir = 'w', idvar = names(dd)[1:2], timevar = names(dd)[3])
  • 啊抱歉没有看到你的评论 re: dcast approach
  • 不,我接受了你的回答。不知道发生了什么。再检查一次
【解决方案2】:

使用 reshape2 的替代方法是

x <- array(1:18, dim = c(3L, 3L, 2L), dimnames = list(
           c("Low", "Medium", "High"), 
           c("Low", "Medium", "High"), 
           c("Yes", "No")))

library(reshape2)
df <- dcast(melt(x), Var1+Var2~Var3) 
df

     Var1   Var2 Yes No
1    Low    Low   1 10
2    Low Medium   4 13
3    Low   High   7 16
4 Medium    Low   2 11
5 Medium Medium   5 14
6 Medium   High   8 17
7   High    Low   3 12
8   High Medium   6 15
9   High   High   9 18

【讨论】:

  • 感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-18
  • 2015-04-14
  • 2013-02-12
  • 1970-01-01
相关资源
最近更新 更多