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


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

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

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

Example

Create the data.table object

Let’s create a data.table object as shown below −

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

Output

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

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

Convert first letter into Capital

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

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

Output

    Students
1:  Sudha
2:  Sudha
3:  Rahul
4:  Rahul
5:  Kunal
6:  Rosy
7:  Anil
8:  Ila
9:  Shaun
10: Kunal
11: Jerry
12: Hidayah
13: Sudha
14: Kunal
15: Teena
16: Yukti
17: Seema
18: Anil
19: Rosy
20: Anil
21: Tom
22: Teena
23: Teena
24: Teena
25: Sam
   Students

Updated on: 15-Nov-2021

317 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements