如何从 R 中的 data.table 对象的列中删除美元符号?
要删除data.tableR中的美元符号对象,我们可以按照以下步骤操作 -
首先,创建一个data.table对象。
然后,使用 gsub 函数和 lapply 函数删除美元符号。
示例
创建data.table对象
让我们创建一个data.table对象,如下所示 -
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(this output will vary on your system due to randomization)-
Product Sale_Price1: 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
删除美元符号
使用 gsub 函数和 lapply 函数从 Sale_Price 列中删除美元符号,如下所示 -
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
Product Sale_Price1: 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
以上是 如何从 R 中的 data.table 对象的列中删除美元符号? 的全部内容, 来源链接: utcz.com/z/341301.html