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
Adaptive Moving Average - Top Performance in R
Find Duplicated Rows (Based on 2 Columns) in Data Frame in R
Add "Filename" Column to Table as Multiple Files Are Read and Bound
What's the Difference Between '1L' and '1'
Duplicates in Multiple Columns
Efficient Way to Filter One Data Frame by Ranges in Another
Geometric Mean: Is There a Built-In
R Convert Zipcode or Lat/Long to County
Multiplying All Elements of a Vector in R
Argument Is of Length Zero in If Statement
Last Observation Carried Forward in a Data Frame
Pass a Vector of Variable Names to Arrange() in Dplyr
Merge and Perfectly Align Histogram and Boxplot Using Ggplot2
What's the Best Way to Use R Scripts on the Command Line (Terminal)
Add Error Bars to Show Standard Deviation on a Plot in R
Moving Average of Previous Three Values in R
Handling Java.Lang.Outofmemoryerror When Writing to Excel from R