r - 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")))
解决方案
我想知道您的结果的 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
推荐阅读
- java - 我尝试了所有可能的组合来通过测试用例,但没有成功。我只通过了一个测试用例,即样品一
- javascript - 当硬编码工作正常时,为什么这个 for 循环在 JavaScript 中不起作用?
- flutter - Flutter:如何在主页中添加 SplashScreen
- android - Recycler ItemDecoration - 未绘制分隔线,不确定原因
- python - 如何使用 PyCharm 在 Python 中优化导入?
- list - Powerquery,字符串是否包含列表中的项目
- java - Spring Boot 中的 @scheduled 注释
- python-3.x - 模型显示 google.protobuf.message.decodeerror: error parsing message
- qt - 如何将动态创建的对象添加到容器中
- java - Eclipse Maven 项目在另一个文件夹中调用类