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