Questions tagged [combn]
121 questions
3
votes
1 answer
purrr - how to apply recursively a function with changing arguments
Ideally I would like to make use of purrr's accumulate function or similar.
Let's say I want to make use of utils::combn function iteratively, and get all the intermediate results (ideally put inside a list of lists).
In example below, initially,…

filemonPi
- 65
- 5
3
votes
1 answer
Finding network links from cluster information
I have network data that looks like this:
data <- data.frame(id = as.character(1:20),
cluster = sample(c("a", "b", "c"), 20, replace = T))
head(data)
# id cluster
# 1 b
# 2 b
# 3 c
# 4 a
# 5 c
# 6 …

Felix T.
- 520
- 3
- 11
3
votes
1 answer
How to create combinations of subsets, such that the final set does not have repeating elements
I am trying to create a subset of a list, covering every possible combination with the condition that final output is the same length as the initial list and there are no repeating elements.
For the list:
X <- c("A","B","C","D")
All the non-null…

constraint_random
- 31
- 2
2
votes
2 answers
How to identify which combinations of two variables whose outputs are below a certain threshold?
I have an R coding question which I want to share. Suppose we have the following:
z1= c(2, 4, 6, 8)
s1= seq(-13, 14, by = 0.1)
s2= seq(-13, 14, by = 0.1)
Function<-function(s1,s2){
Y<-matrix(0,nrow=length(s1),ncol=length(z1))
for (j in…

Blue999
- 23
- 5
2
votes
1 answer
Creating result groups in R, using each element once (combination without repetition)
I have a dataset of 6 individuals: A,B,C,D,E,F
I want to group these into two groups of three individuals and have done so with the combn function in R:
m <- combn(n, 3)
This gives me all 20 possible groups where individuals occur in multiple…

Rgray
- 23
- 3
2
votes
2 answers
In R, how to get the results of combn() from a matrix into a vector without losing data?
I know that combn() can give me all the unique combinations across a vector. However, it gives me a matrix output, and I want it as a vector. Wrapping the output in as.vector() makes every value individual, losing the purpose of running combn() in…

J.Sabree
- 2,280
- 19
- 48
2
votes
2 answers
Count pairs of non-NA observations by row in selected columns
I have a dataframe:
id cog com emo
AUD-002 12 34 24
PAR-044 NA 28 38
BRE-019 0 NA 51
2-1-GRE NA 31 68
I am interested in counting non-NA values per row between all pairs of columns cog, com, emo
My required output is:
id cog com…

Sandy
- 1,100
- 10
- 18
2
votes
1 answer
Get all combination of treatment & control in R
I am using the combn function in R to find the combinations of treatment and control in R, however this function doesn't account for order.
Is there another function similar to combn that could account for combination in different orders?
I hope to…

ppotatomato
- 57
- 6
2
votes
3 answers
Produce all combinations of one element with all other elements within a single column in R
Suppose I have a data frame with a single column that contains letters a, b, c, d, e.
a
b
c
d
e
In R, is it possible to extract a single letter, such as 'a', and produce all possible paired combinations between 'a' and the other letters (with no…

LyricalStats9
- 55
- 5
2
votes
2 answers
Unnesting a combination variable (combn) as a vector
With the following code, I manage to get a fine combination :
tibble(
x = list(c(1, 2, 3), c(4,5,6))
) %>%
mutate(
combination =
x %>%
map(
.f = combn
, 2
) %>%
map(.f = t)
…

Frederi ROSE
- 297
- 2
- 9
2
votes
3 answers
R using combn with apply
I have a data frame that has percentage values for a number of variables and observations, as follows:
obs <- data.frame(Site = c("A", "B", "C"), X = c(11, 22, 33), Y = c(44, 55, 66), Z = c(77, 88, 99))
I need to prepare this data as an edge list…

NickG
- 33
- 5
2
votes
2 answers
How to find all possible combinations of unique vector intersections between multiple (6) vectors of different lengths in R
I have a set of 6 vectors of different lengths (colnames: tp1-tp6). Looks something like this:
tp1 tp2 tp3 tp4 tp5 tp6
K06167 K14521 K17095 K21805 K03238 K18213
K07376 K17095 K01424 K13116 K03283 K14521
…

Johnson Lin
- 21
- 1
2
votes
1 answer
How to apply a combination function on data.table rows to extract and record the different possibilities in another data.table?
I have faced a problem and I could not find a proper method to solve it on previous posts.
I have the data table below which has one column:
listOfRules
1: a, fire, addAfter, b
2: b, storm, addAfter, c
3: c, storm, remove
I'd…
user11733152
2
votes
1 answer
Row products and differences for tibbles created by combn function
I've got a data like below:
df <- structure(list(x1 = c(0.544341260178568, 0.412555423655238, -0.013600925280521,
-0.947831642260442, -0.705819557090343, -0.440052278478676, 0.583360907624305,
-0.548217106316072, -0.381271093402877,…

jakes
- 1,964
- 3
- 18
- 50
2
votes
1 answer
how to `combn` every combination of every element of a vector into a single list
I failed at searching for how to do this specific transformation. Does anyone have a smart way to achieve this? sorry if I missed an obvious answer somewhere. thanks
# start with a single vector
1:3
# achieve this desired structure, containing…

Anthony Damico
- 5,779
- 7
- 46
- 77