Basi c Идея
Поскольку вы намереваетесь сгруппировать элементы по 4 и создать пары в каждой группе, вы фактически делаете это в два этапа:
- Перечислить все комбинации размера 4 с помощью
combn(df, 4, ..., simplify = FALSE)
, где simplify = FALSE
дает результаты в виде списка. - С помощью
combn(...)
мы определяем функцию FUN = function(x) lapply(seq_along(x),function(k) x[c(k,k%%ncol(x)+1)])
или FUN = function(x) lapply(seq_along(x),function(k) x[c(k,k%%length(x)+1)])
, который выполняется для каждой комбинации для получения цепной пары.
Код
combn(df,4,FUN = function(x) lapply(seq_along(x),function(k) x[c(k,k%%ncol(x)+1)]),simplify = FALSE)
такой, что
[[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
Изменить
Если вам нужны только имена столбцов, вы можете попробовать
combn(names(df),4,FUN = function(x) lapply(seq_along(x),function(k) x[c(k,k%%length(x)+1)]),simplify = FALSE)
так, чтобы
[[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"
Данные
set.seed(1)
df <- as.data.frame(matrix(rnorm(40),ncol = 5))