Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The fastest way to convert numeric to character in R

Tags:

r

I need to convert a numeric vector into character in R. As I know, there are different ways (see below).

It seems the fastest ways are sprintf and gettextf.

set.seed(1)
a <- round(runif(100000), 2)
system.time(b1 <- as.character(a))
   user  system elapsed 
  0.108   0.000   0.105 
system.time(b2 <- formatC(a))
   user  system elapsed 
  0.052   0.000   0.052 
system.time(b3 <- sprintf('%.2f', a))
   user  system elapsed 
  0.044   0.000   0.046 
system.time(b4 <- gettextf('%.2f', a))
   user  system elapsed 
  0.048   0.000   0.046 
system.time(b5 <- paste0('', a))
   user  system elapsed 
  0.124   0.000   0.129 

Are there other methods to convert numeric into character in R? Thanks for any suggestions.

like image 904
Bangyou Avatar asked Feb 11 '15 05:02

Bangyou


People also ask

How do I convert numeric to data type in R?

To convert factors to the numeric value in R, use the as. numeric() function. If the input is a vector, then use the factor() method to convert it into the factor and then use the as. numeric() method to convert the factor into numeric values.

How do I convert a column to a character in R?

To convert all columns of the data frame into the character we use apply() function with as. character parameter. The lapply() function applies the given function to the provided data frame.

How do I convert data to string in R?

To convert elements of a Vector to Strings in R, use the toString() function. The toString() is an inbuilt R function used to produce a single character string describing an R object.


1 Answers

Actually it seems like formatC comes out faster:

library(microbenchmark)
a <- round(runif(100000), 2)
microbenchmark(
  as.character(a), 
  formatC(a), 
  format(a), 
  sprintf('%.2f', a), 
  gettextf('%.2f', a), 
  paste0('', a)
)

Output:

Unit: milliseconds
                expr      min       lq     mean   median       uq       max neval
     as.character(a) 69.58868 70.74803 71.98464 71.41442 72.92168  82.21936   100
          formatC(a) 33.35502 36.29623 38.83611 37.60454 39.27079  72.92176   100
           format(a) 55.98344 56.78744 58.00442 57.64804 58.83614  66.15601   100
  sprintf("%.2f", a) 46.54285 47.40126 48.53067 48.10791 49.12717  65.26819   100
 gettextf("%.2f", a) 46.74888 47.81214 49.23166 48.60025 49.16692  84.90208   100
       paste0("", a) 86.62459 88.67753 90.80720 89.86829 91.33774 125.51421   100

My sessionInfo:

R version 3.1.0 (2014-04-10)
Platform: x86_64-apple-darwin13.1.0 (64-bit)

locale:
[1] en_AU.UTF-8/en_AU.UTF-8/en_AU.UTF-8/C/en_AU.UTF-8/en_AU.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] microbenchmark_1.4-2

loaded via a namespace (and not attached):
 [1] colorspace_1.2-4 digest_0.6.4     ggplot2_1.0.0    grid_3.1.0       gtable_0.1.2     MASS_7.3-35     
 [7] munsell_0.4.2    plyr_1.8.1       proto_0.3-10     Rcpp_0.11.3      reshape2_1.4     scales_0.2.4    
[13] stringr_0.6.2    tools_3.1.0    
like image 95
Scott Ritchie Avatar answered Sep 28 '22 06:09

Scott Ritchie