I Del Toro
I Del Toro

Reputation: 943

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

Upvotes: 0

Views: 1523

Answers (2)

Josh O&#39;Brien
Josh O&#39;Brien

Reputation: 162451

If you are primarily interested in relative abundance, you may also want your spp factor to be ordered by frequency. In that case, use reorder() to arrange its levels as desired, and then sort the data.frame so that the species with the most observations come first.

df <- transform(df, spp = reorder(spp, spp, length))
df[order(df$spp, decreasing=TRUE),]
#   spp latitude longitude
# 4   B      4.1      -4.1
# 5   B      5.1      -5.1
# 6   B      6.1      -6.1
# 7   B      7.1      -7.1
# 1   C      1.1      -1.2
# 2   C      2.1      -2.1
# 3   C      3.1      -3.1
# 8   A      8.1      -8.1
# 9   A      9.1      -9.1

## To see one advantage of reordering the factor levels
barplot(table(df$spp))

Upvotes: 1

mnel
mnel

Reputation: 115475

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"

Upvotes: 1

Related Questions