
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
Remove First Character from Column Name in R Data Frame
To remove first character from column name in R data frame, we can use str_sub function of stringr package.
For Example, if we have a data frame called df that contains two columns say XID and XDV then we can remove X from both the column names by using the below mentioned command −
names(df)=str_sub(names(df),2)
Example 1
Following snippet creates a sample data frame −
XColor<-sample(c("Green","Red","Blue"),20,replace=TRUE) YScore<-sample(1:100,20) df1<-data.frame(XColor,YScore) df1
The following dataframe is created
XColor YScore 1 Green 46 2 Red 80 3 Red 22 4 Green 21 5 Blue 54 6 Red 81 7 Blue 41 8 Red 4 9 Blue 63 10 Green 67 11 Green 71 12 Red 65 13 Red 79 14 Blue 58 15 Blue 76 16 Green 10 17 Red 60 18 Red 16 19 Green 27 20 Red 74
To load stringr package and remove first character from column names in df1 on the above created data frame, add the following code to the above snippet −
XColor<-sample(c("Green","Red","Blue"),20,replace=TRUE) YScore<-sample(1:100,20) df1<-data.frame(XColor,YScore) library(stringr) names(df1)=str_sub(names(df1),2) df1
Output
If you execute all the above given snippets as a single program, it generates the following Output −
Color Score 1 Green 46 2 Red 80 3 Red 22 4 Green 21 5 Blue 54 6 Red 81 7 Blue 41 8 Red 4 9 Blue 63 10 Green 67 11 Green 71 12 Red 65 13 Red 79 14 Blue 58 15 Blue 76 16 Green 10 17 Red 60 18 Red 16 19 Green 27 20 Red 74
Example 2
Following snippet creates a sample data frame −
SStudents_ID<-c(200001:200020) LLevel<-sample(c("First","Second","Third"),20,replace=TRUE) df2<-data.frame(SStudents,LLevel) df2
The following dataframe is created
SStudents LLevel 1 200001 First 2 200002 Third 3 200003 Third 4 200004 Second 5 200005 Second 6 200006 Second 7 200007 Second 8 200008 First 9 200009 Second 10 200010 Second 11 200011 First 12 200012 Third 13 200013 Second 14 200014 Second 15 200015 Second 16 200016 First 17 200017 First 18 200018 Third 19 200019 Third 20 200020 Second
To remove first character from column names in df2 on the above created data frame, add the following code to the above snippet −
SStudents_ID<-c(200001:200020) LLevel<-sample(c("First","Second","Third"),20,replace=TRUE) df2<-data.frame(SStudents,LLevel) names(df2)=str_sub(names(df2),2) df2
Output
If you execute all the above given snippets as a single program, it generates the following Output −
Students Level 1 200001 First 2 200002 Third 3 200003 Third 4 200004 Second 5 200005 Second 6 200006 Second 7 200007 Second 8 200008 First 9 200009 Second 10 200010 Second 11 200011 First 12 200012 Third 13 200013 Second 14 200014 Second 15 200015 Second 16 200016 First 17 200017 First 18 200018 Third 19 200019 Third 20 200020 Second