nooshinha
nooshinha

Reputation: 269

How to sort a vector in R, keeping names and values

I have a list like this

                    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 :

Error in [.data.frame(x, order(x, na.last = na.last, decreasing = decreasing)) : undefined columns selected

I also tried this

sort(table(as.data.frame(TTestScores))),decreasing = true)

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?

Upvotes: 2

Views: 7907

Answers (1)

akrun
akrun

Reputation: 887951

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))

Upvotes: 2

Related Questions