kng
kng

Reputation: 649

Sort list of dataframes by vector

I have a list of data frames, called data.list and a vector of the same length called c. The vector c contains only numbers, and I want to sort data.list by c and then chop of all but the top 10 elements of the sorted version of data.list.

I tried the following:

data.list <- order(c, data.list)
data.list <- data.list[1:10]

But the first command just turned data.list into a standard list of numbers. Even the labels for each element of data.list had disappeared. What do I do?

Upvotes: 0

Views: 493

Answers (1)

eddi
eddi

Reputation: 49448

Based on my reading of OP (which lacks details):

l = list(data.frame(a = 1), data.frame(b = 2), data.frame(c = 3), data.frame(d = 4))
v = c(14,12,11,20)

l[order(v)]
#[[1]]
#  c
#1 3
#
#[[2]]
#  b
#1 2
#
#[[3]]
#  a
#1 1
#
#[[4]]
#  d
#1 4

Upvotes: 3

Related Questions