用字符串将1.000更改为1,000

时间:2016-02-29 09:35:31

标签: r comma

我想将var_old转换为var_new。所以我应该将'.'替换为',' Var_old现在是一个字符串,我希望它最终成为数字。

Var_old     Var_new
800         800
285         285
1.100       1,100
900         900
1.000       1,100

首先我尝试了这个:

Var_new <- gsub(".",",", Var_old)
Var_new
",,,"
",,,"
",,,,," 
",,,"
",,,,,"

我试过的其他代码:

Var_new <- sub(".",",", Var_old)
Var_new
",00"
",85"
",.100"
",00"
",.100"

我该怎么做?

1 个答案:

答案 0 :(得分:1)

我们可以使用fixed = TRUE(应该很快)

 sub('.', ',', Var_old, fixed=TRUE)