Convert asis to numeric

hi all,

I tried with the below code to add thousand seperator to dataframe. But the class is getting converted to Asis. Can we make it numeric?

> asd <- format(data.frame(a = c(4324322,45454354)),big.mark=",",scientific=FALSE)
> class(asd$a)
[1] "AsIs"

Hi,

You were almost right :slight_smile:You need to wrap individual columns in the format function, not the whole data frame:

asd = data.frame(a = format(c(4324322,45454354),big.mark=",",scientific=FALSE))
class(asd$a)
#> [1] "character"
asd
#>            a
#> 1  4,324,322
#> 2 45,454,354

Created on 2020-07-29 by the reprex package (v0.3.0)
Hope this helps,
PJ

Thanks. But is there a way to keep it numeric? Right now it is returning as character .

Hi,

Display with commas is only possible in string format, as this is not part of default numeric markup.This would be impossible to use in R, because a vector like this c(100,230) could mean either the value 100230 or two separate values 100 and 230.

So formatting numbers by adding commas will always convert them to strings. Keep that as the last step before you generate the output.

PJ

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.