如何将数组转换为R中的data.table并返回? [英] How to convert array to data.table in R and back?

查看:128
本文介绍了如何将数组转换为R中的data.table并返回?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是将数组转换为数据表的最直接方法吗?

require(data.table)
require(ggplot2)

# this returns a data.table with both array's dimensions and values
aaa <- array(rnorm(3*4*2), dim = c(3,4,2))
DT1 <- as.data.table(as.data.frame.table(aaa))

# the following does not work properly, because it only returns the array values
DT2 <- as.data.table(aaa)


# plot values aggregated by 3rd array dim
ggplot(DT1, aes(Var1, Freq, fill = Var3)) + geom_boxplot()
# sum values by 2nd array dim
DT1[ , sum(Freq), Var2]

EDIT1:
对不起,正确的意思是我得到的数据帧只有一列,因此我不知道值是从原始数组的哪个位置产生的。
的想法是将数组转换为平面表,这样更容易例如使用维度作为因子绘制变量,或按因子汇总值。

sorry, with "properly" I mean that I get a data frame with one column only, so that I don't know from which position in the original array a values has originated. The idea is to transform the array into a flat table, so that is easier to e.g. plot the variables using the dimensions as factors, or to aggregate values by factors. Would that be still possible with DT2?

EDIT2:
另一个有用的事情是将 data.table转换回原始数组。您是否知道通过定义用作维的列来将data.table强制转换为数组的函数?

one other useful thing would be to convert the data.table back into the original array. Do you know a function that coerces data.table to array, by defining which columns to use as dimensions?

aaa <- array(rnorm(3*4*2), dim = c(3,4,2), list(Var1 = LETTERS[1:3], Var2 = LETTERS[1:4], Var3 = LETTERS[1:2] ))

DT1 <- setDT(melt(aaa))

# convert DT1 back to aaa
array(data = DT1[ ,value],
      dim = c(length(unique(DT1[ ,Var1])),
              length(unique(DT1[ ,Var2])),
              length(unique(DT1[ ,Var3]))),
      dimnames = list(Var1 = unique(DT1[ ,Var1]),
                      Var2 = unique(DT1[ ,Var2]),
                      Var3 = unique(DT1[ ,Var3])))

谢谢!

推荐答案

仅适用于1.11.4和1.11.2版本,但不适用于某些较早的版本

返回相同的数据表,但 A = 1 B = 2 C = 3 以及行以不同的方式排序。因此,第二种方法是解决方法。

both approaches essentially return the same data.table but with A=1, B=2, C=3 in your second approach, and rows ordered in different ways. so the second approach is the way to go.

DT2 <- as.data.table(aaa)
head(DT2)
#   V1 V2 V3       value
#1:  1  1  1  0.32337516
#2:  1  1  2  1.59189589
#3:  1  2  1 -1.48751756
#4:  1  2  2 -0.86749305
#5:  1  3  1  0.01017255
#6:  1  3  2  2.66571093

#compare
DT[order(Freq), ]
#and 
DT2[order(value), ]

这篇关于如何将数组转换为R中的data.table并返回?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆