Search code examples
stringrtitle-case

Formatting species names in a column in R


I am working with quite a large database containing a column called 'Species_name' this is a factor column and includes the names of around 40 different species. As R is often case sensitive (particularly when plotting graphs) I was wondering if it was possible to write a line of code which formats all the species names in this column to Capital then lower case i.e. Brown crab, Blonde ray etc.

Apologies for my ignorance - I am new to R!

Many thanks!


Solution

  • You first need to define a function that transforms character values to the case you want. R has built in tolower and toupper but nothing that capitalizes them the way you want.

    capitalize <- function(x){
      first <- toupper(substr(x, start=1, stop=1)) ## capitalize first letter
      rest <- tolower(substr(x, start=2, stop=nchar(x)))   ## everything else lowercase
      paste0(first, rest)
    }
    

    Then you only apply the function to the levels of your factor variable. That's one advantage of factors:

    levels(data$Species_name) <- capitalize(levels(data$Species_name))