在数据表(R)中每三位数字添加逗号

时间:2021-06-19 11:46:41

Suppose my data looks like this:

假设我的数据如下所示:

df1 = data.frame(A=c(1000000.51,5000.33), B=c(0.565,0.794))

I want to use DataTables and have column A be (1,000,001 ; 5,000)

我想使用DataTables并且列A是(1,000,001; 5,000)

library(DT)
datatable(df1)  %>%  formatPercentage('B', 2) %>%
  formatRound('A',digits = 0)

I know i can use scales

我知道我可以使用秤

library(scales)    
comma_format()(1000000)

but I'm not sure how to combine that with DataTables

但我不确定如何将其与DataTables结合起来

Thanks!

2 个解决方案

#1


14  

Had this same issue:

有同样的问题:

Try This:

require(DT)
require(dplyr)

df1 = data.frame(A=c(1000000.51,5000.33, 2500, 251), B=c(0.565,0.794, .685, .456))

df1 <- df1 %>% mutate(A=round(A,digits=0))

datatable(df1)  %>%  formatPercentage('B', 2) %>%
  formatCurrency('A',currency = "", interval = 3, mark = ",")

在数据表(R)中每三位数字添加逗号

#2


3  

You could use formatC

你可以使用formatC

library(dplyr)
library(DT)

df1 %>% 
  mutate(A = formatC(round(A), format = "f", big.mark = ",", drop0trailing = TRUE),
         B = paste0(formatC(100 * B, format = "f", digits = 2), "%")) %>%
  datatable()

Which gives:

在数据表(R)中每三位数字添加逗号

#1


14  

Had this same issue:

有同样的问题:

Try This:

require(DT)
require(dplyr)

df1 = data.frame(A=c(1000000.51,5000.33, 2500, 251), B=c(0.565,0.794, .685, .456))

df1 <- df1 %>% mutate(A=round(A,digits=0))

datatable(df1)  %>%  formatPercentage('B', 2) %>%
  formatCurrency('A',currency = "", interval = 3, mark = ",")

在数据表(R)中每三位数字添加逗号

#2


3  

You could use formatC

你可以使用formatC

library(dplyr)
library(DT)

df1 %>% 
  mutate(A = formatC(round(A), format = "f", big.mark = ",", drop0trailing = TRUE),
         B = paste0(formatC(100 * B, format = "f", digits = 2), "%")) %>%
  datatable()

Which gives:

在数据表(R)中每三位数字添加逗号