假设我有两个带有以下列的数据框。
df1 <- data.frame("A.One"=1:10, "A.Two"=1:10, "A.Three"=1:10)
df2 <- data.frame("B.One"=1:10, "B.Two"=1:10, "B.Three"=1:10)
Run Code Online (Sandbox Code Playgroud)
我想按特定顺序对它们进行 cbind(或正常对它们进行 cbind 并重新排序以实现该顺序):
x <- magic_cbind(df1, df2)
colnames(x)
Run Code Online (Sandbox Code Playgroud)
期望的输出:
A.One B.One A.Two B.Two A.Three B.Three
Run Code Online (Sandbox Code Playgroud)
也就是说,我希望将它们按后缀排序 - 首先我们有所有“一”,然后是所有“二”,根据它们在每个数据帧中最初的顺序。我希望这个解释是有道理的......
这看起来很简单,但我无法弄清楚,我真的不知道要搜索什么,如何调用这个排序......
我们可以用来base R做这个。cbind数据集后,仅order基于每个数据集的串联序列的列
cbind(df1, df2)[order(c(seq_along(df1), seq_along(df2)))]
Run Code Online (Sandbox Code Playgroud)