R:通过列名称向量对数据帧的列进行排序 [英] R: Sort columns of a data frame by a vector of column names

查看:127
本文介绍了R:通过列名称向量对数据帧的列进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框架,如下所示:



其中有1000个具有相似名称的列。



我有一个这样的列名称的向量,如下所示:



该向量按照cluster_id(最多可达11个)进行排序。



我想对数据框中的列进行排序,使列按照



我想要的一个简单的例子是:



数据:

  ABC 
1 2 3
4 5 6


$ b

矢量:
c(B,C,A)

排序:

  BCA 
2 3 1
5 6 4

有快速w ay这样做?

解决方案

Brodie的回答正是你要求的。但是,您意味着您的数据很大,所以我将使用data.table提供一个替代方法,它具有一个名为 setcolorder 的函数,它将通过引用更改列顺序



这是一个可重现的例子。



从一些简单的数据开始:

  mydf<  -  data.frame(A = 1:2,B = 3:4,C = 5:6)
matches& data.frame(X = 1:3,Y = c(C,A,B),Z = 4:6)
mydf
#ABC
# 1 3 5
#2 2 4 6
匹配
#XYZ
#1 1 C 4
#2 2 A 5
#3 3 B 6

提供Brodie的回答的证明:

  out<  -  mydf [matches $ Y] 
out
#CAB
#1 5 1 3
#2 6 2 4

显示更高效的方式来做同样的事情。

  library(data.table)
setDT(mydf)
mydf
#ABC
#1:1 3 5
#2:2 4 6

setcolorder(mydf,as.c hartes(匹配$ Y))
mydf
#CAB
#1:5 1 3
#2:6 2 4
/ pre>

I have a data.frame that looks like this:

which has 1000+ columns with similar names.

And I have a vector of those column names that looks like this:

The vector is sorted by the cluster_id (which goes up to 11).

I want to sort the columns in the data frame such that the columns are in the order of the names in the vector.

A simple example of what I want is that:

Data:

 A    B    C
 1    2    3
 4    5    6

Vector: c("B","C","A")

Sorted:

 B    C    A
 2    3    1
 5    6    4

Is there a fast way to do this?

解决方案

Brodie's answer does exactly what you're asking for. However, you imply that your data are large, so I will provide an alternative using "data.table", which has a function called setcolorder that will change the column order by reference.

Here's a reproducible example.

Start with some simple data:

mydf <- data.frame(A = 1:2, B = 3:4, C = 5:6)
matches <- data.frame(X = 1:3, Y = c("C", "A", "B"), Z = 4:6)
mydf
#   A B C
# 1 1 3 5
# 2 2 4 6
matches
#   X Y Z
# 1 1 C 4
# 2 2 A 5
# 3 3 B 6

Provide proof that Brodie's answer works:

out <- mydf[matches$Y]
out
#   C A B
# 1 5 1 3
# 2 6 2 4

Show a more memory efficient way to do the same thing.

library(data.table)
setDT(mydf)
mydf
#    A B C
# 1: 1 3 5
# 2: 2 4 6

setcolorder(mydf, as.character(matches$Y))
mydf
#    C A B
# 1: 5 1 3
# 2: 6 2 4

这篇关于R:通过列名称向量对数据帧的列进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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