[R] automatic SI prefixes as ticklabels on axis
Rui Barradas
ruipbarradas at sapo.pt
Fri Jan 6 03:10:56 CET 2012
Hello,
You can use your function with 'lapply'
getSIstring(2e7)
getSIstring(2e-8)
lapply(c(2e7, 2e-8, 1234), getSIstring) # as a list
unlist(lapply(c(2e7, 2e-8, 1234), getSIstring)) # as a character vector
or you can include the 'lapply' in the function body
getSIstring2 <- function(x){
f <- function(x){
i=0
repeat{
i=i+1
if (x > prefixpairs$factor[i]) {
sistring <- paste(x/prefixpairs$factor[i],
prefixpairs$prefix[i])
break
}
if (i >= length(prefixpairs$factor)) break
}
sistring
}
prefixpairs <-
data.frame(factor=c(1e24,1e21,1e18,1e15,1e12,1e9,1e6,1e3,1e0,
1e-3,1e-6,1e-9,1e-12,1e-15,1e-18,1e-21,1e-24),
prefix=c("Y", "Z", "E", "P", "T", "G", "M", "k",
" ",
"m", "u", "n", "p", "f", "a", "z", "y"))
x <- sort(x) # needed?
unlist(lapply(x, f))
}
getSIstring2(2e7)
getSIstring2(2e-8)
getSIstring2(c(2e7, 2e-8))
getSIstring2(c(2e7, 2e-8, 1234))
I've included the value 1234 because I coudn't understand wether it could be
passed to the function.
To return '1 k', use a 'round' inside the paste to round the division value.
See ?round
Rui Barradas
--
View this message in context: http://r.789695.n4.nabble.com/automatic-SI-prefixes-as-ticklabels-on-axis-tp4266141p4267911.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list