
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert First Letter of Multiple String Columns to Capital in R Data Frame
To convert first letter of multiple string columns into capital in R data frame, we can follow the below steps −
First of all, create a data frame with string columns.
Then, use sub function along with mutate_each function of dplyr package to convert first letter into capital in string columns.
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"),25,replace=TRUE) Group<--sample(c("first","second","third"),25,replace=TRUE) df<--data.frame(Names,Group) df
Output
On executing, the above script generates the below output(this output will vary on your system due to randomization) −
Names Group 1 sarbat third 2 sarbat first 3 shaun first 4 shaun second 5 ila second 6 sam third 7 john first 8 john third 9 sam third 10 rahul second 11 ila third 12 rahul first 13 teena second 14 john first 15 teena second 16 kunal second 17 sarbat second 18 rahul first 19 ila first 20 john third 21 john second 22 sam third 23 sam first 24 seema first 25 seema second
Convert first letter into capital in multiple columns
Using sub function along with mutate_each function of dplyr package to convert first letter into capital in Names and Group column −
Names<- sample(c("rahul","rosy","hidayah","seema","john","sarbat","shaun","sam","teena","ila","kunal"),25,replace=TRUE) Group<-sample(c("first","second","third"),25,replace=TRUE) df<-data.frame(Names,Group) library(dplyr) df %>% mutate_each(funs(sub("(.)","\U\1", ., perl=TRUE)))
Output
Names Group 1 Rosy First 2 Hidayah Second 3 Sam First 4 Ila Third 5 Kunal Third 6 Teena Third 7 Kunal First 8 Sam First 9 Rosy Second 10 Shaun Second 11 Sarbat Second 12 Teena Third 13 Ila First 14 Shaun Third 15 Hidayah Second 16 Sam Second 17 Rosy Third 18 Hidayah Third 19 Shaun First 20 Sam Second 21 Hidayah Third 22 Sam Second 23 John Second 24 Sam First 25 Sam Second
Advertisements