问题描述
如果我有一个大的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.
谢谢!
推荐答案
尝试一下
df %>% select(noquote(order(colnames(df))))
或者只是
df[,order(colnames(df))]
这篇关于dply:在R中按字母顺序排列列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!