Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uppercase the first letter in data frame

Heres my data,

> data
   Manufacturers       Models
1   audi                RS5  
2   bmw                 M3  
3   cadillac            CTS-V  
4   lexus               ISF

And I would want to uppercase the first letter in the first column, like this:

> data
   Manufacturers       Models
1   Audi                RS5  
2   Bmw                 M3  
3   Cadillac            CTS-V  
4   Lexus               ISF

I would appreciate any help on this question. Thanks a lot.

like image 497
Bruce Brown Avatar asked Apr 27 '13 07:04

Bruce Brown


People also ask

How do you capitalize the first letter in a data frame?

Applying capitalize() function We apply the str. capitalize() function to the above dataframe for the column named Day. As you can notice, the name of all the days are capitalized at the first letter.

How do you make the first letter capital in python?

The first letter of a string can be capitalized using the capitalize() function. This method returns a string with the first letter capitalized. If you are looking to capitalize the first letter of the entire string the title() function should be used.


1 Answers

Taking the example from the documentation for ?toupper and modifying it a bit:

capFirst <- function(s) {
    paste(toupper(substring(s, 1, 1)), substring(s, 2), sep = "")
}

data$Manufacturers <- capFirst(data$Manufacturers)
> data
#   Manufacturers Models
# 1          Audi    RS5
# 2           Bmw     M3
# 3      Cadillac  CTS-V
# 4         Lexus    ISF
like image 66
Arun Avatar answered Sep 27 '22 23:09

Arun