1

I have dataframe which looks like:

Var1    Var2      Var3     Var4
A       100000    5000     15000
B       1000000   2500     200

I want to convert them as below mentioned dataframe.

Required Output:

Var1    Var2        Var3     Var4
A       1,00,000    5,000     15,000
B       10,00,000   2,500     200
pogibas
  • 27,303
  • 19
  • 84
  • 117
Roy1245
  • 507
  • 4
  • 18

2 Answers2

2

You can use the prettyNum function :

prettyNum("100000",big.interval = 3,  big.mark = ",")
[1] "100,000"

To do this conversion data-frame-wide, you can go for :

library(purrr)
map_df(a, prettyNum ,big.interval = 3,  big.mark = ",")
# A tibble: 2 x 4
Var1  Var2      Var3  Var4  
<chr> <chr>     <chr> <chr> 
1 A     100,000   5,000 15,000
2 B     1,000,000 2,500 200   
Colin FAY
  • 4,849
  • 1
  • 12
  • 29
0

You can use comma from the scales package to convert to commas. To run on all columns on the dataframe we can wrap it in an sapply function:

sapply(df, function(x) scales::comma(x)) 

     Var1 Var2        Var3    Var4    
[1,] "A"  "100,000"   "5,000" "15,000"
[2,] "B"  "1,000,000" "2,500" "200"    
Michael Harper
  • 14,721
  • 2
  • 60
  • 84