user3101468
user3101468

Reputation: 23

re-order data after split in R

I have used the split function to group my data with the results as follow:

$H1050
     row.names    Avg  Tree
1071      1904 2.8530 H1050
1072      1905 2.4030 H1050
1073      1906 1.6030 H1050
1074      1907 1.7660 H1050
1075      1908 2.5150 H1050

$H1040
     row.names    Avg  Tree
1737       1664 4.3200 H1040
1738       1665 4.0900 H1040
1739       1666 5.3100 H1040
1740       1667 4.8000 H1040
1741       1668 3.7000 H1040
1742       1669 4.0400 H1040
1743       1670 4.2800 H1040

There are around 36 groups in total. I am then looping through this data to plot each individual data group. I would like to be able to re-order the split data by the number of elements in the row.names column. For example, count the number of elements in the row.names column and then the group with the highest number should be at the top and descending accordingly.

What is the best approach here?

Thanks very much for your time and effort!

Upvotes: 2

Views: 1781

Answers (2)

Ricardo Saporta
Ricardo Saporta

Reputation: 55350

Assuming splat is your split data set:

  splat[order(sapply(splat, function(x) length(x[["row.names"]])))]

Upvotes: 1

josliber
josliber

Reputation: 44320

Something like this (assuming a list named l):

num.row.names = lapply(l, function(x) length(unique(x$row.names)))
num.row.names = do.call(c, num.row.names)
l = l[order(num.row.names, decreasing=T)]

If row.names is the names of the rows in the split data frames instead of a variable in the data frames (I'm not clear which it is from the OP), you would replace the first line with:

num.row.names = lapply(l, function(x) length(unique(row.names(x))))

Upvotes: 1

Related Questions