Questions tagged [combn]
121 questions
19
votes
5 answers
Faster version of combn
Is there a way to speed up the combn command to get all unique combinations of 2 elements taken from a vector?
Usually this would be set up like this:
# Get latest version of data.table
library(devtools)
install_github("Rdatatable/data.table", …

majom
- 7,863
- 7
- 55
- 88
14
votes
3 answers
Find all possible teams in R
Feels like this should be straightforward but I have been through stack overflow and combn help, and cannot see solution.
The below players need to be in teams of 3 versus 2. I need to find all possible combinations of teams. For example two…

luciano
- 13,158
- 36
- 90
- 130
12
votes
3 answers
Intersect all possible combinations of list elements
I have a list of vectors:
> 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"…

enricoferrero
- 2,249
- 1
- 23
- 28
7
votes
3 answers
Generate all unique combinations from a vector with repeating elements
This questions was asked previously but only for vectors with non-repeating elements. I was not able to find an easy solution to get all combinations from a vector with repeating elements. To illustrate I listed an example below.
x <- c('red',…

Jian
- 365
- 1
- 6
- 19
6
votes
3 answers
R: how to perform more complex calculations from a combn of a dataset?
Right now, I have a combn from the built in dataset iris. So far, I have been guided into being able to find the coefficient of lm() of the pair of values.
myPairs <- combn(names(iris[1:4]), 2)
formula <- apply(myPairs, MARGIN=2, FUN=paste,…

Luke Zhang
- 343
- 1
- 4
- 14
6
votes
1 answer
Get all possible combinations by row in matrix
I have an m x n matrix that looks like this:
1 2 3
4 5 6
What is the fastest way to get all possible combinations by row? In this case, that would be c(1,4), c(1,5), c(1,6), c(2,4), c(2,5) ... c(3,5), c(3,6)
How do I solve this using a vectorized…

user3294195
- 1,748
- 1
- 19
- 36
5
votes
2 answers
Efficiently find the overlap between two time intervals in R
I have used combn() to find the overlap between two dates/times using lubridate package. But combn() is too slow to process the large dataset I am working on. I am trying to use comboGeneral() from RcppAlgos package but I can't get it to work. Any…

Zaw
- 1,434
- 7
- 15
5
votes
3 answers
R find all possible unique combinations
I am trying to find all possible unique combinations in R. It seems that there have been a lot of similar questions asked, but I was not able to find the same one.
My question is to find combinations of m number of elements from vector x, but m…

Jian
- 365
- 1
- 6
- 19
5
votes
2 answers
Find all the sums of all combinations of 3 rows from 5 columns
I've loaded a table from a .CVS file using
mydata = read.csv("CS2Data.csv") # read csv file
which gave me:
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 …

JimJ
- 51
- 1
5
votes
3 answers
Possible combinations of a vector with respect order
x <- letters[1:4]
x
# [1] "a" "b" "c" "d"
t(combn(x, 2))
# [,1] [,2]
# [1,] "a" "b"
# [2,] "a" "c"
# [3,] "a" "d"
# [4,] "b" "c"
# [5,] "b" "d"
# [6,] "c" "d"
How should I write the code if I also what inverse combinations with b-a,…

Tomas Ericsson
- 347
- 1
- 2
- 10
4
votes
2 answers
Calculating combinations of column names without duplicates using combn
I have two dataframes with 3 columns each and each dataframe consists of different data types (df1 has continuous data with column name suffix "con", df2 has categorical data with column name suffix "cat")
My data:
df1 <- data.frame(t1_con=c(1:5),…

LHordley
- 95
- 5
4
votes
3 answers
Convert combn output to matrix of type 'dist'
I have a dataframe in the following form:
dim1 dim2
1 Loc.1 0.325
2 Loc.2 0.325
3 Loc.3 0.321
4 Loc.4 0.256
5 Loc.5 0.255
I would like to compute the mean of each combination of two (2) elements within 'dim2'; and convert the output into a…

Chrys
- 313
- 3
- 10
4
votes
1 answer
Duplicating & modifying rows of a dataframe dependent on observations [R]
This is a follow up to this question: Duplicating observations of a dataframe, but also replacing specific variable values in R
I have tried to write as succinctly as possible, whilst giving all necessary information. In this current example, I…

jalapic
- 13,792
- 8
- 57
- 87
4
votes
2 answers
Use data.table in R to add multiple columns to a data.table with = with only one function call
This is a direkt expansion of this Question.
I have a dataset and I want to find all pairwise combinations of Variable v depending on Variables x and y:
library(data.table)
DT = data.table(x=rep(c("a","b","c"),each=6), y=c(1,1,6), v=1:18)
x…

fc9.30
- 2,293
- 20
- 19
3
votes
6 answers
All Combinations of Numbers That Meet a Condition
I am working with the R programming language.
Suppose I have numbers: 2010,2011,2012,2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020
My Question: I want to find out all possible pairs of these numbers where the difference is greater than 1. E.g.…

stats_noob
- 5,401
- 4
- 27
- 83