简体   繁体   English

通过每个数据帧中的行数对数据帧列表进行重新排序

[英]Reorder a list of dataframes by the number of rows in each dataframe

Say I have three dataframes in my.list , each with different numbers of rows. 假设我在my.list有三个数据my.list ,每个数据my.list具有不同的行数。 I would like to reorder this list so that the first element of the list is the dataframe with the highest number of rows (in the below example, d2 ). 我想对该列表重新排序,以使列表的第一个元素是具有最大行数的数据框(在下面的示例中, d2 )。

d1 <- data.frame(y1 = c(1, 2, 3),
                 y2 = c(4, 5, 6))
d2 <- data.frame(y1 = c(3, 2, 1, 3, 2),
                 y2 = c(6, 5, 4, 2, 5))
d3 <- data.frame(y1 = c(2, 1),
                 y2 = c(3, 2))

my.list <- list(d1, d2, d3)

The expected output: 预期输出:

str(mylist[[1]]) ## 'data.frame':   5 obs. of  2 variables:
                     $ y1: num  3 2 1 3 2
                     $ y2: num  6 5 4 2 5

The reason for this: I'm repeatedly plotting data from the first element in several lists of dataframes, and would like to make sure I'm plotting the dataframes with the most data points when I call plot(my.list[[1]]) . 这样做的原因是:我反复绘制多个数据帧列表中第一个元素的数据,并希望确保在调用plot(my.list[[1]])时,绘制数据点最多的数据帧。 plot(my.list[[1]])

Probably a cleaner solution would be to, within the plot call, search for the element/dataframe with the highest number of rows and plot that, but I'm not sure how easy that would be. 可能更干净的解决方案是在plot调用中搜索具有最大行数的元素/数据框并进行绘制,但是我不确定这样做有多容易。

One potentially complicating factor is that there will occasionally be a list of dataframes where there is more than one dataframe sharing the highest number of rows. 一个潜在的复杂因素是,偶尔会有一列数据框,其中有多个共享最大行的数据框。 In that case, it wouldn't matter which one is called--they'd both do fine--but I'm not sure whether that creates an issue here. 在那种情况下,哪一个被调用都没关系-两者都可以-但我不确定这是否会造成问题。

假设您的列表称为“ lst”

lst= lst[order(sapply(lst,nrow),decreasing = T)]

Use vapply to get the number of rows in each data frame, then use rev(order(...)) to sort them from most rows to least. 使用vapply获取每个数据帧中的行数,然后使用rev(order(...))对大多数行到最少行进行排序。

nrow_each <- vapply(my.list, nrow, numeric(1))
my.list[rev(order(nrow_each))]

Or do it in one, difficult-to-read line 还是一本难读的书

my.list[rev(order(vapply(my.list, nrow, numeric(1))))]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM