看过另一篇关于列名和combn函数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调用后保留列名,这样在这种情况下,我们可以得到"x,y“而不是"X1.5,X4.8”,如下所示?
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发布于 2015-01-22 22:14:32
我怀疑你真的想用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 5https://stackoverflow.com/questions/28090716
复制相似问题