Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to remove percentage sign at last position from every value in R data frame column?
To remove percentage sign at last position from every value in R data frame column, we can follow the below steps −
First of all, create a data frame with a column having percent sign at last position in every value.
Then, use gsub function to remove the percent sign at last position from every value in the column.
Example
Create the data frame
Let’s create a data frame as shown below −
var<-sample(c("5%","10%","1%","6%","7%","12%"),25,replace=TRUE)
df<-data.frame(var)
df
Output
On executing, the above script generates the below output(this output will vary on your system due to randomization) −
var 1 10% 2 12% 3 1% 4 1% 5 6% 6 5% 7 7% 8 5% 9 1% 10 6% 11 10% 12 10% 13 12% 14 10% 15 10% 16 5% 17 6% 18 12% 19 7% 20 6% 21 10% 22 12% 23 7% 24 10% 25 1%
Remove percentage sign from last position
Using gsub function to remove the percentage sign at last position from every value in column var of data frame df as shown below −
var<-sample(c("5%","10%","1%","6%","7%","12%"),25,replace=TRUE)
df<-data.frame(var)
df$new_var<-gsub("%$","",df$var)
df
Output
var new_var 1 10% 10 2 12% 12 3 1% 1 4 1% 1 5 6% 6 6 5% 5 7 7% 7 8 5% 5 9 1% 1 10 6% 6 11 10% 10 12 10% 10 13 12% 12 14 10% 10 15 10% 10 16 5% 5 17 6% 6 18 12% 12 19 7% 7 20 6% 6 21 10% 10 22 12% 12 23 7% 7 24 10% 10 25 1% 1
Advertisements
