首页 > 解决方案 > R将向量的三个值的所有组合添加到三维数组

问题描述

我有一个包含两列的数据框。第一个“V1”表示找到第二列“V2”的不同项目的对象,例如:

V1 <- c("A", "A", "A", "A", "B", "B", "B", "C", "C", "C", "C")
V2 <- c("a","b","c","d","a","c","d","a","b","d","e")
df <- data.frame(V1, V2)

例如,“A”包含“a”、“b”、“c”和“d”。我正在寻找的是一个三维数组,其维度为length(unique(V2))(以及名称“a”到“e”为dimnames)。

对于 I 的每个唯一值,V1我想要三个V2项目的所有可能组合(例如,对于“A”,它将是c("a", "b", "c")c("a", "b", "d"c("b", "c", "d").

这些“三项共现”中的每一个都应被视为三维数组中的一个坐标,因此应将其添加到数组中的值应显示的频率计数中。结果应该是以下数组

ar <- array(data     = c(0,0,0,0,0,0,0,1,2,1,0,1,0,2,0,0,2,2,0,1,0,1,0,1,0,
                         0,0,1,2,1,0,0,0,0,0,1,0,0,1,0,2,0,1,0,1,1,0,0,1,0,
                         0,1,0,2,0,1,0,0,1,0,0,0,0,0,0,2,1,0,0,0,0,0,0,0,0,
                         0,2,2,0,1,2,0,1,0,1,2,1,0,0,0,0,0,0,0,0,1,1,0,0,0,
                         0,1,0,1,0,1,0,0,1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0),
            dim      = c(5, 5, 5),
            dimnames = list(c("a", "b", "c", "d", "e"),
                            c("a", "b", "c", "d", "e"),
                            c("a", "b", "c", "d", "e")))

标签: rarrays

解决方案


我想知道您的结果的 3D 对称性。我花了一段时间才明白你想要拥有所有组合的所有排列。

library(gtools) #for the permutations

foo <- function(x) {
  #all combinations:
  combs <- combn(x, 3, simplify = FALSE) 
  #all permutations for each of the combinations:
  combs <- do.call(rbind, lapply(combs, permutations, n = 3, r = 3)) 
  #tabulate:
  do.call(table, lapply(asplit(combs, 2), factor, levels = letters[1:5]))
}

#apply grouped by V1, then sum the results
res <- Reduce("+", tapply(df$V2, df$V1, foo))

#check
all((res - ar)^2 == 0)
#[1] TRUE

推荐阅读