Search code examples
rsortinglevels

Reordering data and making a re-ordered list in r


I previously asked this question:"

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 received a great answer which worked well:

# 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),decreasing = TRUE]

but now I am stuck again in making an object"species" which has the ordered species names in it. Right now I have:

species <- levels(df$spp)

this command puts everything back in alphabetical order but I need the object to be ordered by "n" (the number of records). Any suggestions. Thanks in advance!

Cheers, Israel


Solution

  • This is as easy as using unique. I coerce to character so as not to get confused between levels and actual values.

     unique(as.character(dfordered$spp))
    

    To get here from the original data, use table

    table(df$spp)
    
    ##  A B C 
    ##  2 4 3 
    

    You can sort this

    sppCount <- table(df$spp)
    
    names(sort(sppCount, decreasing = TRUE))
    
    # [1] "B" "C" "A"