再次重新排列数据框中的列 [英] Reordering columns in data frame once again
问题描述
我想对数据框中的列进行重新排序,但是到目前为止发现的结果并不令人满意。
I want to re-order my columns in my data frame, but what I found so far is not satisfactory.
我的数据框如下:
cnt <-as.factor(c("Country 1", "Country 2", "Country 3", "Country 1", "Country 2", "Country 3" ))
bnk <-as.factor(c("bank 1", "bank 2", "bank 3", "bank 1", "bank 2", "bank 3" ))
mayData <-data.frame(age=c(10,12,13,10,11,15), Country=cnt, Bank=bnk, q10=c(1,1,1,2,2,2),q11=c(1,1,1,2,2,2), q1=c(1,1,1,2,2,2), q9=c(1,1,1,2,2,2), q6=c(1,1,1,2,2,2), year=c(1950,1960,1970,1980,1990,2000) )
age Country Bank q10 q11 q1 q9 q6 year
1 10 Country 1 bank 1 1 1 1 1 1 1950
2 12 Country 2 bank 2 1 1 1 1 1 1960
3 13 Country 3 bank 3 1 1 1 1 1 1970
4 10 Country 1 bank 1 2 2 2 2 2 1980
5 11 Country 2 bank 2 2 2 2 2 2 1990
6 15 Country 3 bank 3 2 2 2 2 2 2000
,但我想重新排列以下列:
but I want to re-arrange the columns to look like this:
Country Bank year age q1 q6 q9 q10 q11
1 Country 1 bank 1 1950 10 1 1 1 1 1
2 Country 2 bank 2 1960 12 1 1 1 1 1
3 Country 3 bank 3 1970 13 1 1 1 1 1
4 Country 1 bank 1 1980 10 2 2 2 2 2
5 Country 2 bank 2 1990 11 2 2 2 2 2
6 Country 3 bank 3 2000 15 2 2 2 2 2
我的真实数据框有很多列,因此,使用索引或每列的名称手动重新排列列顺序并不是最佳选择。
My real dataframe has a lot of columns, so rearranging the column orders "manually" using the index or the names of each column is not optimal.
还请注意,对于以 q
s开头的列名,我希望它们按升序排列,从 q1
到 q11
。问题是R无法理解 q6
(代表问题6)应该在 q10
之前。要查看此缺陷,请看以下示例:
Notice also, that for the column names that begin with q
s I want to have them in ascending order, that is from q1
to q11
. The problem is that R fails to understand that q6
- which stands for "question 6" - should be precede q10
. To see this deficiency, look at the following example:
mayData<-mayData[,order(colnames(mayData),decreasing=F)]
age Bank Country q1 q10 q11 q6 q9 year
1 10 bank 1 Country 1 1 1 1 1 1 1950
2 12 bank 2 Country 2 1 1 1 1 1 1960
3 13 bank 3 Country 3 1 1 1 1 1 1970
4 10 bank 1 Country 1 2 2 2 2 2 1980
5 11 bank 2 Country 2 2 2 2 2 2 1990
6 15 bank 3 Country 3 2 2 2 2 2 2000
我想对列进行重新排序是先根据自己的喜好以一些灵活的方式对一些列进行排序,然后使用递减的排序标准。但是,R可以理解的逻辑变量,可以正确地对 q
进行排序。
So, essentially the way I want to reorder my columns is to first sort a few columns in some flexible way according to my preference and then use a decreasing ordering criteria. But, the "logical" one, one that R can understand to sort the q
s properly.
推荐答案
我们可以使用 gtools中的
排列 q列。 mixedsort
We can use mixedsort
from gtools
to arrange the 'q' columns.
library(gtools)
i1 <- grep("q\\d+", names(mayData))
nm1 <- mixedsort(names(mayData)[i1])
mayData[c(setdiff(names(mayData), nm1), nm1)]
# age Country Bank year q1 q6 q9 q10 q11
#1 10 Country 1 bank 1 1950 1 1 1 1 1
#2 12 Country 2 bank 2 1960 1 1 1 1 1
#3 13 Country 3 bank 3 1970 1 1 1 1 1
#4 10 Country 1 bank 1 1980 2 2 2 2 2
#5 11 Country 2 bank 2 1990 2 2 2 2 2
#6 15 Country 3 bank 3 2000 2 2 2 2 2
注意:仅使用基本R
函数和单个包。
NOTE: Using only base R
functions and a single package.
或者如@Cath所述,删除子字符串带有 gsub
的订单也可以用于订购
Or as @Cath mentioned, removing the substring with gsub
can be used to order as well
sort(as.numeric(sub("^q", "", names(mayData)[i1])))
这篇关于再次重新排列数据框中的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!