dply:在 R 中按字母顺序排列列 [英] dply: order columns alphabetically in R

查看:20
本文介绍了dply:在 R 中按字母顺序排列列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个大的 DF(数百个)列,其中的 col_names 按字母顺序随机分布:

If I have a large DF (hundreds and hundreds) columns with different col_names randomly distributed alphabetically:

df.x <- data.frame(2:11, 1:10, rnorm(10))
colnames(df.x) <- c("ID", "string", "delta")

我将如何按 col_name 字母顺序(垂直)对所有数据进行排序?

How would I order all of the data (vertically) by col_name alphabetically?

本质上,我有数百个 CSV(sep="|") 文本文件,我需要将它们的列读入单个 df,按字母顺序排列这些列,然后使用其他一些 dplyf 函数来获得最终结果.除了如何按字母顺序排列列之外,我已经弄清楚了所有这些.我不想按字母表对列(向上和向下)进行排序,而是按照 col_names 及其相应数据的实际垂直方向.类似于在 Excel 中剪切和粘贴整列数据.

Essentially, I have hundreds of CSV(sep="|") text files that I need to read their columns into a single df, order those columns alphabetically and then use some other dplyf functions to get a final result. I have all of this figured out except how to order the columns alphabetically. I do not want to sort the columns (up and down) by alphabet, rather, the actual vertical orientation of the col_names and their corresponding data. Analogous to cutting and pasting entire columns of data in Excel.

例如,我回顾了这种方法,但这是按字母顺序对行进行排序",这不是我想要做的.

For example I reviewed this approach but this is the "sort" the rows alphabetically bit, which is not what I'm looking to do.

如何按列对数据框进行排序(s)?

谢谢!

推荐答案

试试这个

df %>% select(noquote(order(colnames(df))))

或者只是

df[,order(colnames(df))]

这篇关于dply:在 R 中按字母顺序排列列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆