且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在列之间拆分数据框,例如在每第 n 列?

更新时间:2023-11-18 23:26:40

每 4 列拆分 Mydata.我们可以显式使用 use split.default:

To split Mydata every 4 columns. We can use explicitly use split.default:

split.default(Mydata, rep(1:3, each = 4))

默认"方法可以按列拆分数据框.只需根据需要设置分组变量即可.

The "default" method can split a data frame by columns. Just set the grouping variable by your need.

对于平衡分组,gl 很方便(参见 ?gl).我们可以使用gl(3, 4)代替上面的rep(1:3, 4),这样可以避免从整数"到因子"的类型转换.

For balanced grouping, gl is handy (see ?gl). We can use gl(3, 4) instead of rep(1:3, 4) in the above, which avoids type conversion from "integer" to "factor".

一般来说,对于每n列"使用gl(ncol(Mydata)/n, n)(n必须除以ncol(Mydata)).

In general, use gl(ncol(Mydata) / n, n) for "every n columns" (n must divide ncol(Mydata)).