Number formatting in R -
i have r vector a = c(.45652356112, .0000063983562)
. , want format numbers in vector such resulting vector c(.46, 6.4e-06)
. when do
format(a, digits=2, nsmall=2)
then result c(4.6e-01, 6.4e-06)
rather want. apparently, each of elements should passed format()
function separately desired result. however, since length of original a
50000, loop calls format()
each element way slow. how can desired formatting without loop?
you can put format inside ifelse
condition
ifelse(a < 0.001, format(a, digits = 2, nsmall = 2), round(a, 2)) # [1] "0.46" "6.4e-06"