- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to remove a character in an R data frame column?
To remove a character in an R data frame column, we can use gsub() function which will replace the character with blank. For example, if we have a data frame called df that contains a character column say x which has a character ID in each value then it can be removed by using the command gsub("ID","",as.character(df$x)).
Example1
Consider the below data frame −
> x1<-sample(c("Male1","Female1"),20,replace=TRUE) > x2<-rpois(20,5) > df1<-data.frame(x1,x2) > df1
Output
x1 x2 1 Male1 8 2 Female1 4 3 Male1 9 4 Male1 2 5 Male1 7 6 Female1 5 7 Male1 3 8 Female1 6 9 Female1 4 10 Female1 3 11 Male1 5 12 Male1 4 13 Female1 7 14 Male1 6 15 Female1 5 16 Male1 3 17 Female1 10 18 Female1 4 19 Male1 7 20 Female1 3
Removing 1 from all values in x1 −
> df1$x1<-gsub("1","",as.character(df1$x1)) > df1
Output
x1 x2 1 Male 8 2 Female 4 3 Male 9 4 Male 2 5 Male 7 6 Female 5 7 Male 3 8 Female 6 9 Female 4 10 Female 3 11 Male 5 12 Male 4 13 Female 7 14 Male 6 15 Female 5 16 Male 3 17 Female 10 18 Female 4 19 Male 7 20 Female 3
Example2
> y1<-sample(c("Hot#","Cold#"),20,replace=TRUE) > y2<-rnorm(20) > df2<-data.frame(y1,y2) > df2
Output
y1 y2 1 Cold# 0.1725073 2 Hot# -0.4692023 3 Hot# 2.4386160 4 Cold# -1.3592685 5 Hot# 0.4500866 6 Cold# -0.7023989 7 Cold# 0.7383042 8 Hot# -1.7325754 9 Hot# 0.2053521 10 Hot# -2.0123750 11 Cold# -0.7203218 12 Hot# 1.2389079 13 Cold# -0.6401144 14 Cold# -0.6136728 15 Hot# 1.2877643 16 Cold# 0.4291966 17 Cold# 1.8316996 18 Hot# 0.8165865 19 Cold# -0.3978465 20 Cold# -0.9352063
Removing # from all values in y1 −
> df2$y1<-gsub("#","",as.character(df2$y1)) > df2
Output
y1 y2 1 Cold 0.1725073 2 Hot -0.4692023 3 Hot 2.4386160 4 Cold -1.3592685 5 Hot 0.4500866 6 Cold -0.7023989 7 Cold 0.7383042 8 Hot -1.7325754 9 Hot 0.2053521 10 Hot -2.0123750 11 Cold -0.7203218 12 Hot 1.2389079 13 Cold -0.6401144 14 Cold -0.6136728 15 Hot 1.2877643 16 Cold 0.4291966 17 Cold 1.8316996 18 Hot 0.8165865 19 Cold -0.3978465 20 Cold -0.9352063
Advertisements