How to convert first letter into capital in single column R data frame using a function?


To convert first letter into capital in single column data frame in R, we can follow the below steps −

  • First of all, create a data frame with string column.

  • Then, use capitalize function from R.utils package to convert first letter into capital in single column.

Example

Create the data frame

Let’s create a data frame as shown below −

Names<-
sample(c("rahul","rosy","hidayah","seema","john","sarbat","shaun","sam","teena","ila","kunal","sudha","anil","yukti","jerry","tom"),25,replace=TRUE)
df<-data.frame(Names)
df

Output

On executing, the above script generates the below output(this output will vary on your system due to randomization) −

   Names
1  yukti
2  john
3  anil
4  shaun
5  shaun
6  rahul
7  ila
8  kunal
9  anil
10 jerry
11 hidayah
12 yukti
13 shaun
14 teena
15 tom
16 seema
17 jerry
18 teena
19 jerry
20 hidayah
21 shaun
22 hidayah
23 sam
24 rosy
25 seema

Convert first letter into Capital

Using capitalize function from R.utils package to convert first letter into capital in Names column −

Names<-
sample(c("rahul","rosy","hidayah","seema","john","sarbat","shaun","sam","teena","ila"," kunal","sudha","anil","yukti","jerry","tom"),25,replace=TRUE)
df<-data.frame(Names)
library(R.utils)
df$Names<-capitalize(df$Names)
df

Output

   Names
1  Ila
2  Rosy
3  Ila
4  Kunal
5  Jerry
6  Seema
7  Rosy
8  Sam
9  Ila
10 Sudha
11 Seema
12 Jerry
13 Tom
14 Rosy
15 Shaun
16 John
17 Ila
18 Tom
19 Rosy
20 Hidayah
21 Rosy
22 Sarbat
23 John
24 Sarbat
25 Jerry

Updated on: 10-Nov-2021

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements