如何删除R数据框中的美元符号?
要删除 R 数据框中的美元符号,我们可以按照以下步骤操作 -
首先,创建一个数据框。
然后,使用 gsub 函数和 lapply 函数删除美元符号。
示例
创建数据框
让我们创建一个数据框,如下所示 -
Product<-sample(c("Milk","Sugar","Bread","Wheat"),25,replace=TRUE)输出结果Price<-
sample(c("$10","$5","$12","$15","$9","$8","$11","$14","$13"),25,replace=TRUE)
df<-data.frame(Product,Price)
df
执行时,上述脚本生成以下内容output(this output will vary on your system due to randomization)-
Product Price1 Milk $11
2 Sugar $5
3 Milk $5
4 Bread $10
5 Sugar $8
6 Sugar $11
7 Sugar $5
8 Wheat $15
9 Sugar $5
10 Wheat $15
11 Wheat $5
12 Milk $8
13 Milk $13
14 Wheat $13
15 Wheat $5
16 Milk $13
17 Wheat $12
18 Bread $5
19 Bread $10
20 Milk $9
21 Milk $14
22 Bread $11
23 Milk $8
24 Sugar $10
25 Bread $8
删除美元符号
使用 gsub 函数和 lapply 函数从价格列中删除美元符号,如下所示 -
Product<-sample(c("Milk","Sugar","Bread","Wheat"),25,replace=TRUE)输出结果Price<-
sample(c("$10","$5","$12","$15","$9","$8","$11","$14","$13"),25,replace=TRUE)
df<-data.frame(Product,Price)
df[]<-lapply(df,gsub,pattern="$",fixed=TRUE,replacement="")
df
Product Price1 Sugar 15
2 Sugar 14
3 Wheat 8
4 Sugar 10
5 Milk 11
6 Milk 5
7 Sugar 11
8 Sugar 14
9 Milk 12
10 Wheat 10
11 Wheat 15
12 Bread 11
13 Bread 13
14 Milk 5
15 Bread 13
16 Wheat 11
17 Wheat 13
18 Sugar 10
19 Sugar 12
20 Milk 9
21 Milk 13
22 Wheat 15
23 Sugar 9
24 Bread 14
25 Milk 5
以上是 如何删除R数据框中的美元符号? 的全部内容, 来源链接: utcz.com/z/358351.html