Katarina_1
Katarina_1

Reputation: 13

Creating groups of certain size in R according to matrix values

I have 30 specimens and I would like to test the interactions between them. I can test 4 interactions at the same time (1st with 2nd, 2nd with 3rd, 3rd with 4th and 4th with 1st). I would like to figure out optimal groups of 4 pairewise interactions.

I created matrix of all pairewise interactions:

combinations1 <- combn (specimens, 2, fun = NULL, smiplify = TRUE)

Now I would like to create groups of size 4, where the second element of first pair has the same value as the first element of second pair: (a,b), (b,c), (c,d), (d,a).

I tried with apply and outer, however I do not know how to write the function to get desired results. Is it also possible with aggregate?

I am new to R and programing, so sorry in advance. Thank you! :)

Upvotes: 1

Views: 147

Answers (1)

ThomasIsCoding
ThomasIsCoding

Reputation: 101753

Basic Idea

Since you intend to group things by 4 and make chain-wise pairs within each group, you actually do it via two steps:

  • Enumerate all combinations of size 4, via combn(df, 4, ..., simplify = FALSE), where simplify = FALSE gives results in a list.
  • With in combn(...), we define a function FUN = function(x) lapply(seq_along(x),function(k) x[c(k,k%%ncol(x)+1)]) or FUN = function(x) lapply(seq_along(x),function(k) x[c(k,k%%length(x)+1)]), which is executed for each combination to produce chain-wise pair.

Code

combn(df,4,FUN = function(x) lapply(seq_along(x),function(k) x[c(k,k%%ncol(x)+1)]),simplify = FALSE)

such that

[[1]]
[[1]][[1]]
          V1          V2
1 -0.6264538  0.57578135
2  0.1836433 -0.30538839
3 -0.8356286  1.51178117
4  1.5952808  0.38984324
5  0.3295078 -0.62124058
6 -0.8204684 -2.21469989
7  0.4874291  1.12493092
8  0.7383247 -0.04493361

[[1]][[2]]
           V2          V3
1  0.57578135 -0.01619026
2 -0.30538839  0.94383621
3  1.51178117  0.82122120
4  0.38984324  0.59390132
5 -0.62124058  0.91897737
6 -2.21469989  0.78213630
7  1.12493092  0.07456498
8 -0.04493361 -1.98935170

[[1]][[3]]
           V3          V4
1 -0.01619026  0.61982575
2  0.94383621 -0.05612874
3  0.82122120 -0.15579551
4  0.59390132 -1.47075238
5  0.91897737 -0.47815006
6  0.78213630  0.41794156
7  0.07456498  1.35867955
8 -1.98935170 -0.10278773

[[1]][[4]]
           V4         V1
1  0.61982575 -0.6264538
2 -0.05612874  0.1836433
3 -0.15579551 -0.8356286
4 -1.47075238  1.5952808
5 -0.47815006  0.3295078
6  0.41794156 -0.8204684
7  1.35867955  0.4874291
8 -0.10278773  0.7383247


[[2]]
[[2]][[1]]
          V1          V2
1 -0.6264538  0.57578135
2  0.1836433 -0.30538839
3 -0.8356286  1.51178117
4  1.5952808  0.38984324
5  0.3295078 -0.62124058
6 -0.8204684 -2.21469989
7  0.4874291  1.12493092
8  0.7383247 -0.04493361

[[2]][[2]]
           V2          V3
1  0.57578135 -0.01619026
2 -0.30538839  0.94383621
3  1.51178117  0.82122120
4  0.38984324  0.59390132
5 -0.62124058  0.91897737
6 -2.21469989  0.78213630
7  1.12493092  0.07456498
8 -0.04493361 -1.98935170

[[2]][[3]]
           V3          V5
1 -0.01619026  0.38767161
2  0.94383621 -0.05380504
3  0.82122120 -1.37705956
4  0.59390132 -0.41499456
5  0.91897737 -0.39428995
6  0.78213630 -0.05931340
7  0.07456498  1.10002537
8 -1.98935170  0.76317575

[[2]][[4]]
           V5         V1
1  0.38767161 -0.6264538
2 -0.05380504  0.1836433
3 -1.37705956 -0.8356286
4 -0.41499456  1.5952808
5 -0.39428995  0.3295078
6 -0.05931340 -0.8204684
7  1.10002537  0.4874291
8  0.76317575  0.7383247


[[3]]
[[3]][[1]]
          V1          V2
1 -0.6264538  0.57578135
2  0.1836433 -0.30538839
3 -0.8356286  1.51178117
4  1.5952808  0.38984324
5  0.3295078 -0.62124058
6 -0.8204684 -2.21469989
7  0.4874291  1.12493092
8  0.7383247 -0.04493361

[[3]][[2]]
           V2          V4
1  0.57578135  0.61982575
2 -0.30538839 -0.05612874
3  1.51178117 -0.15579551
4  0.38984324 -1.47075238
5 -0.62124058 -0.47815006
6 -2.21469989  0.41794156
7  1.12493092  1.35867955
8 -0.04493361 -0.10278773

[[3]][[3]]
           V4          V5
1  0.61982575  0.38767161
2 -0.05612874 -0.05380504
3 -0.15579551 -1.37705956
4 -1.47075238 -0.41499456
5 -0.47815006 -0.39428995
6  0.41794156 -0.05931340
7  1.35867955  1.10002537
8 -0.10278773  0.76317575

[[3]][[4]]
           V5         V1
1  0.38767161 -0.6264538
2 -0.05380504  0.1836433
3 -1.37705956 -0.8356286
4 -0.41499456  1.5952808
5 -0.39428995  0.3295078
6 -0.05931340 -0.8204684
7  1.10002537  0.4874291
8  0.76317575  0.7383247


[[4]]
[[4]][[1]]
          V1          V3
1 -0.6264538 -0.01619026
2  0.1836433  0.94383621
3 -0.8356286  0.82122120
4  1.5952808  0.59390132
5  0.3295078  0.91897737
6 -0.8204684  0.78213630
7  0.4874291  0.07456498
8  0.7383247 -1.98935170

[[4]][[2]]
           V3          V4
1 -0.01619026  0.61982575
2  0.94383621 -0.05612874
3  0.82122120 -0.15579551
4  0.59390132 -1.47075238
5  0.91897737 -0.47815006
6  0.78213630  0.41794156
7  0.07456498  1.35867955
8 -1.98935170 -0.10278773

[[4]][[3]]
           V4          V5
1  0.61982575  0.38767161
2 -0.05612874 -0.05380504
3 -0.15579551 -1.37705956
4 -1.47075238 -0.41499456
5 -0.47815006 -0.39428995
6  0.41794156 -0.05931340
7  1.35867955  1.10002537
8 -0.10278773  0.76317575

[[4]][[4]]
           V5         V1
1  0.38767161 -0.6264538
2 -0.05380504  0.1836433
3 -1.37705956 -0.8356286
4 -0.41499456  1.5952808
5 -0.39428995  0.3295078
6 -0.05931340 -0.8204684
7  1.10002537  0.4874291
8  0.76317575  0.7383247


[[5]]
[[5]][[1]]
           V2          V3
1  0.57578135 -0.01619026
2 -0.30538839  0.94383621
3  1.51178117  0.82122120
4  0.38984324  0.59390132
5 -0.62124058  0.91897737
6 -2.21469989  0.78213630
7  1.12493092  0.07456498
8 -0.04493361 -1.98935170

[[5]][[2]]
           V3          V4
1 -0.01619026  0.61982575
2  0.94383621 -0.05612874
3  0.82122120 -0.15579551
4  0.59390132 -1.47075238
5  0.91897737 -0.47815006
6  0.78213630  0.41794156
7  0.07456498  1.35867955
8 -1.98935170 -0.10278773

[[5]][[3]]
           V4          V5
1  0.61982575  0.38767161
2 -0.05612874 -0.05380504
3 -0.15579551 -1.37705956
4 -1.47075238 -0.41499456
5 -0.47815006 -0.39428995
6  0.41794156 -0.05931340
7  1.35867955  1.10002537
8 -0.10278773  0.76317575

[[5]][[4]]
           V5          V2
1  0.38767161  0.57578135
2 -0.05380504 -0.30538839
3 -1.37705956  1.51178117
4 -0.41499456  0.38984324
5 -0.39428995 -0.62124058
6 -0.05931340 -2.21469989
7  1.10002537  1.12493092
8  0.76317575 -0.04493361

Edit

If you need the column names only, you can try

combn(names(df),4,FUN = function(x) lapply(seq_along(x),function(k) x[c(k,k%%length(x)+1)]),simplify = FALSE)

such that

[[1]]
[[1]][[1]]
[1] "V1" "V2"

[[1]][[2]]
[1] "V2" "V3"

[[1]][[3]]
[1] "V3" "V4"

[[1]][[4]]
[1] "V4" "V1"


[[2]]
[[2]][[1]]
[1] "V1" "V2"

[[2]][[2]]
[1] "V2" "V3"

[[2]][[3]]
[1] "V3" "V5"

[[2]][[4]]
[1] "V5" "V1"


[[3]]
[[3]][[1]]
[1] "V1" "V2"

[[3]][[2]]
[1] "V2" "V4"

[[3]][[3]]
[1] "V4" "V5"

[[3]][[4]]
[1] "V5" "V1"


[[4]]
[[4]][[1]]
[1] "V1" "V3"

[[4]][[2]]
[1] "V3" "V4"

[[4]][[3]]
[1] "V4" "V5"

[[4]][[4]]
[1] "V5" "V1"


[[5]]
[[5]][[1]]
[1] "V2" "V3"

[[5]][[2]]
[1] "V3" "V4"

[[5]][[3]]
[1] "V4" "V5"

[[5]][[4]]
[1] "V5" "V2"

Data

set.seed(1)
df <- as.data.frame(matrix(rnorm(40),ncol = 5))

Upvotes: 1

Related Questions