I Del Toro
I Del Toro

Reputation: 943

Reodering a dataframe in r

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

Upvotes: 1

Views: 113

Answers (1)

mnel
mnel

Reputation: 115392

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

Upvotes: 3

Related Questions