如何通过属性“名称"对向量进行排序在R中,但保持第一个元素不变? [英] How do I order a vector by attribute "names" in R but keeping the first element constant?
本文介绍了如何通过属性“名称"对向量进行排序在R中,但保持第一个元素不变?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
说我有一个向量及其名称,如下所示:
Say I have a vector and its name as follows:
vct <- c(67, "apple", 88, "orange", 71)
names(vct) <- c("c1", "b2", "d66", "a65", "a11")
现在,我希望第一个元素67保持原样,并按其名称的顺序对其余元素进行排序.这样看来:"67","71",橙色",苹果","88".
Now I want the first element 67 to be as is and order the rest on the order of their names. So that it appears: "67", "71", "orange", "apple", "88".
推荐答案
您可以使用 -1
排除第一个,其余按其名称排序,加1和 c
与1:
You can use -1
for to exclude the first one, order the rest by it's name, add 1 and c
with 1:
vct[c(1, order(names(vct)[-1])+1)]
# c1 a11 a65 b2 d66
# "67" "71" "orange" "apple" "88"
这篇关于如何通过属性“名称"对向量进行排序在R中,但保持第一个元素不变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文