感觉这应该很简单,但我已经经历过堆栈溢出和combn帮助,但看不到解决方案。
下面的玩家需要组成 3 对 2 的队伍。我需要找到所有可能的队伍组合。例如,两个可能的团队是“Ross”、“Bobby”和“Casper”在一个团队中,“Max”和“Jake”在另一团队中。我该如何编码?
players <- c("Ross", "Bobby", "Max", "Casper", "Jake")
Run Code Online (Sandbox Code Playgroud) 有没有办法加快combn命令,以获得从矢量中取出的2个元素的所有独特组合?
通常这将设置如下:
# Get latest version of data.table
library(devtools)
install_github("Rdatatable/data.table", build_vignettes = FALSE)
library(data.table)
# Toy data
d <- data.table(id=as.character(paste0("A", 10001:15000)))
# Transform data
system.time({
d.1 <- as.data.table(t(combn(d$id, 2)))
})
Run Code Online (Sandbox Code Playgroud)
但是,combn使用data.table计算所有可能的组合要慢10倍(23秒对比我的计算机3秒).
system.time({
d.2 <- d[, list(neighbor=d$id[-which(d$id==id)]), by=c("id")]
})
Run Code Online (Sandbox Code Playgroud)
处理非常大的向量,我正在寻找一种通过仅计算唯一组合(如combn)来节省内存的方法,但是使用data.table的速度(参见第二个代码片段).
我感谢任何帮助.
我有一个向量列表:
> l <- list(A=c("one", "two", "three", "four"), B=c("one", "two"), C=c("two", "four", "five", "six"), D=c("six", "seven"))
> l
$A
[1] "one" "two" "three" "four"
$B
[1] "one" "two"
$C
[1] "two" "four" "five" "six"
$D
[1] "six" "seven"
Run Code Online (Sandbox Code Playgroud)
我想计算列表元素的所有可能的成对组合之间的重叠长度,即(结果的格式无关紧要):
AintB 2
AintC 2
AintD 0
BintC 1
BintD 0
CintD 1
Run Code Online (Sandbox Code Playgroud)
我知道combn(x, 2)可以用来得到一个矢量中所有可能的成对组合的矩阵,length(intersect(a, b))这会给我两个向量重叠的长度,但我想不出把这两个东西放在一起的方法.
任何帮助深表感谢!谢谢.
我有一个mx n矩阵,如下所示:
1 2 3
4 5 6
Run Code Online (Sandbox Code Playgroud)
按行获取所有可能组合的最快方法是什么?在这种情况下,那将是c(1,4), c(1,5), c(1,6), c(2,4), c(2,5) ... c(3,5), c(3,6)
如何使用矢量化方法解决这个问题?通常,mx n矩阵将具有n^m这样的组合.
现在,我有一个来自内置数据集iris的组合.到目前为止,我已被引导能够找到这对值的lm()系数.
myPairs <- combn(names(iris[1:4]), 2)
formula <- apply(myPairs, MARGIN=2, FUN=paste, collapse="~")
model <- lapply(formula, function(x) lm(formula=x, data=iris)$coefficients[2])
model
Run Code Online (Sandbox Code Playgroud)
但是,我想进一步使用lm()中的系数来进一步计算.我想做这样的事情:
Coefficient <- lm(formula=x, data=iris)$coefficients[2]
Spread <- myPairs[1] - coefficient*myPairs[2]
library(tseries)
adf.test(Spread)
Run Code Online (Sandbox Code Playgroud)
该过程本身很简单,但我还没有找到一种方法来为数据集中的每个组合执行此操作.(作为旁注,adf.test不适用于此类数据,但我只是使用虹膜数据集进行演示).我想知道,为这样的程序编写一个循环会更好吗?
我曾经使用packagecombn()来查找两个日期/时间之间的重叠。但处理我正在处理的大型数据集太慢了。我正在尝试从包中使用,但无法让它工作。任何帮助,将不胜感激。如果您知道我应该查看的任何其他包/功能,也请告诉我。lubridatecombn()comboGeneral()RcppAlgos
get_overlap <- function(.data, .id, .start, .end) {
id <- .data[[.id]]
int <- interval(.data[[.start]], .data[[.end]])
names <- combn(id, 2, FUN = function(.) paste(., collapse = "-"))
setNames(combn(int, 2, function(.) intersect(.[1], .[2])), names)
}
get_overlap(dat, "id", "start", "end")
# a-b a-c a-d a-e b-c b-d b-e c-d c-e d-e
# 49 1 4 17 23 14 18 NA 2 NA
Run Code Online (Sandbox Code Playgroud)
这是我使用失败的尝试comboGeneral()。
comboGeneral(dat$int, 2, FUN = function(.) intersect(.[1], .[2]))
# Output: …Run Code Online (Sandbox Code Playgroud) 我已经使用 .CVS 文件加载了一个表
mydata = read.csv("CS2Data.csv") # read csv file
Run Code Online (Sandbox Code Playgroud)
这给了我:
mydata
Date DCM TMUS SKM RCI SPOK
1 11/2/2015 -0.88 -2.16 -1.04 1.12 0.67
2 12/1/2015 1.03 3.26 -2.25 -5.51 -0.23
3 1/4/2016 1.94 1.29 0.13 -1.16 0.11
4 2/1/2016 -0.41 -2.94 0.99 3.93 -0.19
5 3/1/2016 -0.68 1.27 -0.79 -2.06 -0.33
6 4/1/2016 1.82 1.22 -0.05 -1.27 -0.46
7 5/2/2016 -0.36 3.40 0.63 -2.77 0.46
8 6/1/2016 1.94 0.77 0.51 -0.26 1.66
9 7/1/2016 0.12 3.18 1.84 -1.34 …Run Code Online (Sandbox Code Playgroud) 之前曾问过这个问题,但仅针对具有非重复元素的向量。我找不到一个简单的解决方案来从具有重复元素的向量中获取所有组合。为了说明,我在下面列出了一个示例。
x <- c('red', 'blue', 'green', 'red', 'green', 'red')
Run Code Online (Sandbox Code Playgroud)
向量x具有3个“红色”重复元素和2个“绿色”重复元素。所有唯一组合的预期结果将是这样。
# unique combinations with one element
'red'
'blue'
'green'
# unique combination with two elements
'red', 'blue' # same as 'blue','red'
'red', 'green'
'red', 'red'
'blue', 'green'
'green', 'green'
# unique combination with three elements
'red', 'blue', 'green'
'red', 'red', 'blue'
'red', 'red', 'green'
'red', 'red', 'red' # This is valid because there are three 'red's
'green', 'green', 'red'
'green', 'green', 'blue'
# more unique combinations with four, five, and …Run Code Online (Sandbox Code Playgroud) 这是对这个问题的直接扩展.我有一个数据集,我想根据变量x和y找到变量v的所有成对组合:
library(data.table)
DT = data.table(x=rep(c("a","b","c"),each=6), y=c(1,1,6), v=1:18)
x y v
1: a 1 1
2: a 1 2
3: a 6 3
4: a 1 4
5: a 1 5
6: a 6 6
7: b 1 7
8: b 1 8
9: b 6 9
10: b 1 10
11: b 1 11
12: b 6 12
13: c 1 13
14: c 1 14
15: c 6 15
16: c 1 16
17: c 1 17 …Run Code Online (Sandbox Code Playgroud) 我试图在 R 中找到所有可能的独特组合。似乎有很多类似的问题被问到,但我找不到相同的问题。
我的问题是从向量 x 中找到 m 个元素的组合,但 m 可能大于 x。例如,从字母 [1:2] 中选取 3 个元素,希望可以返回:
combn(letters[1:2],3)
[,1] [,2] [,3] [,4]
[1,] "a" "a" "a" "b"
[2,] "a" "a" "b" "b"
[3,] "a" "b" "b" "b"
Run Code Online (Sandbox Code Playgroud)
但是组合函数 n < m 中的错误。有类似的功能,包括 gtools:permutations、expand.grid。
如果之前有人问过同样的问题,但我没有听懂,再次道歉。谢谢。
combn ×10
r ×10
combinations ×5
data.table ×2
intersect ×1
list ×1
loops ×1
lubridate ×1
matrix ×1
permutation ×1