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 dollar sign from a column of a data.table object in R?
To remove dollar sign in data.table object in R, we can follow the below steps −
First of all, create a data.table object.
Then, use gsub function along with lapply function to remove dollar sign.
Example
Create the data.table object
Let’s create a data.table object as shown below −
library(data.table)
Sale_Price<-sample(c("10 $","5 $","12 $","15 $","9 $"),25,replace=TRUE)
Product_Type<-sample(c("Digital","Physical"),25,replace=TRUE)
DT<-data.table(Product,Sale_Price)
DT
Output
On executing, the above script generates the below output(this output will vary on your system due to randomization) −
Product Sale_Price 1: Milk 15 $ 2: Sugar 15 $ 3: Sugar 12 $ 4: Bread 12 $ 5: Milk 15 $ 6: Sugar 15 $ 7: Sugar 9 $ 8: Bread 10 $ 9: Bread 9 $ 10: Wheat 12 $ 11: Wheat 15 $ 12: Milk 9 $ 13: Wheat 10 $ 14: Wheat 12 $ 15: Milk 10 $ 16: Milk 10 $ 17: Milk 10 $ 18: Milk 10 $ 19: Sugar 5 $ 20: Milk 15 $ 21: Wheat 5 $ 22: Sugar 12 $ 23: Milk 9 $ 24: Wheat 9 $ 25: Bread 12 $ Product Sale_Price
Remove dollar sign
Using gsub function along with lapply function to remove dollar sign from Sale_Price column as shown below −
library(data.table)
Sale_Price<-sample(c("10 $","5 $","12 $","15 $","9 $"),25,replace=TRUE)
Product_Type<-sample(c("Digital","Physical"),25,replace=TRUE)
DT<-data.table(Product,Sale_Price)
DT[]<-lapply(DT,gsub,pattern=" $",fixed=TRUE,replacement="")
DT
Output
Product Sale_Price 1: Milk 15 2: Sugar 15 3: Sugar 12 4: Bread 12 5: Milk 15 6: Sugar 15 7: Sugar 9 8: Bread 10 9: Bread 9 10: Wheat 12 11: Wheat 15 12: Milk 9 13: Wheat 10 14: Wheat 12 15: Milk 10 16: Milk 10 17: Milk 10 18: Milk 10 19: Sugar 5 20: Milk 15 21: Wheat 5 22: Sugar 12 23: Milk 9 24: Wheat 9 25: Bread 12 Product Sale_Price
Advertisements
