在查看有关列名称和组合函数here的其他帖子后,请考虑相同的data.frame。我们用所有2个可能的向量进行组合:
foo <- data.frame(x=1:5,y=4:8,z=10:14, w=8:4)
all_comb <- combn(foo,2)
有没有办法在combn调用之后保留列名,所以在这种情况下我们可以得到&#34; x y&#34;而不是&#34; X1.5 X4.8&#34;如下图所示?
comb_df <- data.frame(all_comb[1,1],all_comb[2,1])
print(comb_df)
X1.5 X4.8
1 1 4
2 2 5
3 3 6
4 4 7
5 5 8
答案 0 :(得分:0)
我怀疑您确实想要使用expand.grid()
。
试试这个:
head(expand.grid(foo))
x y z w
1 1 4 10 8
2 2 4 10 8
3 3 4 10 8
4 4 4 10 8
5 5 4 10 8
6 1 5 10 8
或
head(expand.grid(foo[, 1:2]))
x y
1 1 4
2 2 4
3 3 4
4 4 4
5 5 4
6 1 5