如何获取2的数据帧的列的所有组合? [英] How to obtain all combinations of the columns of a data frame taken by 2?
本文介绍了如何获取2的数据帧的列的所有组合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有这个数据框:
矩阵(c(2,4,3,1,5,7, 1,2,3,5,8,2,4,5,1,1,3,6,1,3,4,5,6,1),nrow = 6,ncol = 4,byrow = TRUE) - > X
pre>
as.data.frame(X) - > X.df
V1 V2 V3 V4
1 2 4 3 1
2 5 7 1 2
3 3 5 8 2
4 4 5 1 1
5 3 6 1 3
6 4 5 6 1
那么我想获得一个包含所有列的组合的数据帧的列表,没有重复,并避免使用自己的列。这意味着,具有以下标题的数据框列表:
V1,V2
V1,V3
V1,V4
V2,V3
V2,V4
V3,V4
解决方案
> combn(name(X.df),2,simplified = FALSE)
[[1]]
V1 V2
1 2 4
2 5 7
3 3 5
4 4 5
5 3 6
6 4 5
[[2]]
V1 V3
1 2 3
2 5 1
3 3 8
4 4 1
5 3 1
6 4 6
[[3]] $ b $ V1 V4
1 2 1
2 5 2
3 3 2
4 4 1
5 3 3
6 4 1
[[4]]
V2 V3
1 4 3
2 7 1
3 5 8
4 5 1
5 6 1
6 5 6
[[5]]
V2 V4
1 4 1
2 7 2
3 5 2
4 5 1
5 6 3
6 5 1
[[6]]
V3 V4
1 3 1
2 1 2
3 8 2
4 1 1
5 1 3
6 6 1
Suppose I have this data frame:
matrix(c(2,4,3,1,5,7,1,2,3,5,8,2,4,5,1,1,3,6,1,3,4,5,6,1),nrow=6,ncol=4,byrow = TRUE)->X as.data.frame(X)->X.df V1 V2 V3 V4 1 2 4 3 1 2 5 7 1 2 3 3 5 8 2 4 4 5 1 1 5 3 6 1 3 6 4 5 6 1
then I would like to obtain a list of a set of data frames containing all combinations of columns taken by 2, without repetition, and avoiding any column with itself. That means, a list of dataframes with the following headers:
V1,V2 V1,V3 V1,V4 V2,V3 V2,V4 V3,V4
Any idea of how to do this?
解决方案> combn(names(X.df),2,simplify=FALSE) [[1]] V1 V2 1 2 4 2 5 7 3 3 5 4 4 5 5 3 6 6 4 5 [[2]] V1 V3 1 2 3 2 5 1 3 3 8 4 4 1 5 3 1 6 4 6 [[3]] V1 V4 1 2 1 2 5 2 3 3 2 4 4 1 5 3 3 6 4 1 [[4]] V2 V3 1 4 3 2 7 1 3 5 8 4 5 1 5 6 1 6 5 6 [[5]] V2 V4 1 4 1 2 7 2 3 5 2 4 5 1 5 6 3 6 5 1 [[6]] V3 V4 1 3 1 2 1 2 3 8 2 4 1 1 5 1 3 6 6 1
这篇关于如何获取2的数据帧的列的所有组合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文