R长表到多维数组

时间:2019-02-11 02:27:03

标签: r data.table transform

我有一个长格式的表格:

require(data.table)
sampleDT <- data.table(Old = c("A","B","A","B","A","B","A","B")
                       , New = c("A","A","B","B","A","A","B","B")
                       , Time = c(1,1,1,1,2,2,2,2)
                       , value1 = c(1,1,1,1,1,1,1,1)
                       , value2 = c(0,0,0,0,0,0,0,0))
print(sampleDT)

   Old New Time value1 value2
1:   A   A    1      1      0
2:   B   A    1      1      0
3:   A   B    1      1      0
4:   B   B    1      1      0
5:   A   A    2      1      0
6:   B   A    2      1      0
7:   A   B    2      1      0
8:   B   B    2      1      0

我想转换为3维数组。就像是: enter image description here

基本上,我们会将“新,旧,时间”列作为我们的三个维度。 每个单元格的值是某种函数的输出,这些函数的输入为“ value1,value2”。

在这种情况下,当Time = 1时,结果为:

matrix(data = c(1, 1+0, 0, -0), nrow = 2, ncol = 2, byrow = FALSE)
     [,1] [,2]
[1,]    1    0
[2,]    1    0

如何实现?

由于我们正在处理相对较大的数据集,因此内存使用量和计算时间是重要的考虑因素。

1 个答案:

答案 0 :(得分:1)

尝试使用xtabs():

sampleDT <- data.frame(Old = c("A","B","A","B","A","B","A","B"), 
                       New = c("A","A","B","B","A","A","B","B"),
                       Time = c(1,1,1,1,2,2,2,2),
                       value1 = c(1,1,1,1,1,1,1,1),
                       value2 = c(0,0,0,0,0,0,0,0))

Value1 <- xtabs(value1 ~ Old + New + Time, sampleDT, drop = FALSE)
Value2 <- xtabs(value2 ~ Old + New + Time, sampleDT, drop = FALSE)

is.array(Value1)
is.array(Value2)

Value1[, 2,] <- 0 # Sets all second columns to zero for Value1
Value2[1,,] <- 0 # Idem with first row for Value2
Value2[2,2,] <- Value2[2,2,] * (-1)

Result <- Value1 + Value2

Result

, , Time = 1

   New
Old A B
  A 1 0
  B 1 0

, , Time = 2

   New
Old A B
  A 1 0
  B 1 0

希望有帮助。