如何在R中对向量排序,并保留名称和值 [英] How to sort a vector in R, keeping names and values
问题描述
我有一个这样的列表
TTestScores
ALQ120 1.5587910512
ALQ130 -1.7682950999
ALQ140 1.9618228701
ALQ150 4.6912035774
AUQ191 3.7440051368
AUQ260 1.4148585857
AUQ270 3.0977853206
ARQ010 -2.2057617248
ARQ020 -2.2057617248
...
我想按降序对它进行排序.我尝试过
I want to sort this in decreasing order. I tried
sort(as.data.frame(TTestScores))
但是我遇到了这个错误:
but I got this error :
[.data.frame
(x,order(x,na.last = na.last,减少= 减少)):未定义的列已选中
Error in
[.data.frame
(x, order(x, na.last = na.last, decreasing = decreasing)) : undefined columns selected
我也尝试过
sort(table(as.data.frame(TTestScores))),decreasing = true)
可以,但是不能以降序排列.除了使用第二种语法外,我还将分数作为表的名称,这使得无法理解哪个分数属于哪个变量(ALQ120,...). 有人可以告诉我如何对同时包含变量名称和分数的列表进行排序吗?
which works but it doesn't sort in decreasing order. besides when I use the second syntax, I get the scores as the names of the table which makes it impossible to understand which score belongs to which variable(ALQ120,...). anybody can tell me how to sort this list having both the name of variables and their scores?
推荐答案
在dput
中,"TTestScores"是vector
.我们可以直接将sort
与decreasing=TRUE
一起创建data.frame
From the dput
, the 'TTestScores' is a vector
. We can directly sort
with decreasing=TRUE
and create a data.frame
data.frame(V1=sort(TTestScores, decreasing=TRUE))
这篇关于如何在R中对向量排序,并保留名称和值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!