Comma Separator for Numbers in R

Comma separator for numbers in R?

You can try either format or prettyNum, but both functions return a vector of characters. I'd only use that for printing.

> prettyNum(12345.678,big.mark=",",scientific=FALSE)
[1] "12,345.68"
> format(12345.678,big.mark=",",scientific=FALSE)
[1] "12,345.68"

EDIT: As Michael Chirico says in the comment:

Be aware that these have the side effect of padding the printed strings with blank space, for example:

> prettyNum(c(123,1234),big.mark=",")
[1] " 123" "1,234"

Add trim=TRUE to format or preserve.width="none" to prettyNum to prevent this:

> prettyNum(c(123,1234),big.mark=",", preserve.width="none")
[1] "123" "1,234"
> format(c(123,1234),big.mark=",", trim=TRUE)
[1] "123" "1,234"

Format number in R with both comma thousands separator and specified decimals

format not formatC:

format(round(as.numeric(1000.64), 1), nsmall=1, big.mark=",") # 1,000.6

How to read data when some numbers contain commas as thousand separator?

I want to use R rather than pre-processing the data as it makes it easier when the data are revised. Following Shane's suggestion of using gsub, I think this is about as neat as I can do:

x <- read.csv("file.csv",header=TRUE,colClasses="character")
col2cvt <- 15:41
x[,col2cvt] <- lapply(x[,col2cvt],function(x){as.numeric(gsub(",", "", x))})

Return a number as a comma-delimited string

Unfortunately, a number of Renjin packages are not fully implemented, so until a fix is in place, the code in the question is a decent work around:

commas <- function( n ) {
s <- sprintf( "%03.0f", n %% 1000 )
n <- n %/% 1000

while( n > 0 ) {
s <- concat( sprintf( "%03.0f", n %% 1000 ), ',', s )
n <- n %/% 1000
}

gsub( '^0*', '', s )
}

format all numeric columns with comma to seprate the v

format() does this.

library(tidyverse)

df <- tibble(
Customers = c("babs", "bobs", "pint", "red", "yellow"),
telex = c(3434323424, 53545322, 43, 35435, 4567),
manay = c(937387573, 758464938, 7453537384, 624353, 44),
players = c(96222221, 122134, 223444, 345654, 334)
)

df <- df %>%
mutate(across(telex:players, ~ format(., big.mark = ",", scientific = F)))

But note that your data changes to type "character".

Format a number in Indian currency format

I don't know of any native way to do this, but the following function will achieve it for you:

nums <- function(n) {
dec <- round(n %% 1, 2)
dec <- ifelse(dec < 0.01, "", substr(dec, 2, 4))
int <- n %/% 1
ints <- vapply(int, function(x) {
x <- as.character(x)
len <- nchar(x)
if(len <= 3) return(x)
rev_x <- paste(rev(unlist(strsplit(x, ""))), collapse = "")
str <- paste0(substr(rev_x, 1, 3), ",")
str2 <- substr(rev_x, 4, 100)
str2 <- gsub("(\\d{2})", "\\1,", str2)
rev_x <- paste0(str, str2)
return(paste(rev(unlist(strsplit(rev_x, ""))), collapse = ""))
}, character(1))

return(sub("^,", "", paste0(ints, dec)))
}

You can use it like this:

nums(c(1234.12, 342, 35123251.12))
#> [1] "1,234.12" "342" "3,51,23,251.12"


Related Topics



Leave a reply



Submit