1

How do I re-order a dataframe with multiple species in r. Each species has a different number of observations and I need the final dataframe to be ordered in descending order with the species with most observations listed first. In this example the final dataframe should look list specie B first then Species C and finally specie A.

colA= c("C","C","C","B","B","B","B","A","A")
colB= c(1.1,2.1,3.1,4.1,5.1,6.1,7.1,8.1,9.1)
colC= c(-1.2,-2.1,-3.1,-4.1,-5.1,-6.1,-7.1,-8.1,-9.1)
df= data.frame (spp=colA, latitude=colB, longitude=colC)
df
I Del Toro
  • 913
  • 4
  • 15
  • 36
  • If I wanted to make a list of the species ordered the way the species are in the df (i.e. B,C,A). What command can I use? Rightnow I have `species <- levels(df$spp)`. How can I order the species in the new "species" object? – I Del Toro Mar 12 '13 at 02:05

1 Answers1

3

You will have to create a column that you wish to order by

A base solution

# add a column counting the number of rows in each species
df <- transform(df, n  = ave(latitude ,spp, FUN = length))

# order by this new column

dfordered <- df[order(df$n),]

A data.table solution (coding and memory efficiency)

library(data.table)
DT <- data.table(df)

DT[, n := .N, by = spp]

DTordered <-   DT[order(n),]
# or
setkey(DT, n)

Sorting data.frames by columns is covered in detail in How to sort a dataframe by column(s)?

Community
  • 1
  • 1
mnel
  • 113,303
  • 27
  • 265
  • 254
  • Great! Thanks for the input. To reoder in decensing order would it just be (dfordered <- df[order(df$n),]) – I Del Toro Mar 12 '13 at 01:06
  • @IDelToro `df[order(-df$n),])` or `df[order(df$n,decreasing = TRUE),])` see http://stackoverflow.com/questions/1296646/how-to-sort-a-dataframe-by-columns-in-r for more details on the ordering part of the question, which is a duplicate. – mnel Mar 12 '13 at 01:09
  • One more quick follow up question. If I wanted to make a list of the species ordered the way the species are in the df (i.e. B,C,A). What command can I use? Rightnow I have `species <- levels(df$spp)`. How can I order the species in the new "species" object? – I Del Toro Mar 12 '13 at 01:44