On 2012-01-05, Jonas Stein <n...@jonasstein.de> wrote: > i want to plot values with frequency on a logarithmic x axis. > similar to this example that i found in the web: > http://www.usspeaker.com/jensen%20p15n-graph.gif > > I would like to convert long numbers to si prefix notation > like in the example > > (200000 to 200k, 35000000 to 3.5 M) > > Of course i could create labels by hand, but > i have many files so i need some automatic function. > > Has anyone done that in R? > > Kind regards and thank you for your help,
my first try looks like this: ================================== getSIstring <- function(x){ sistring <- paste(x); prefixpairs <- data.frame(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), c("Y", "Z", "E", "P", "T", "G", "M", "k", " ", "m", "u", "n", "p", "f", "a", "z", "y")) colnames(prefixpairs) <- c("factor", "prefix") 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} return(sistring) } ================================== > getSIstring(2e7) [1] "20 M" How can i improve this function? It would be nice if it could handle lists too like sin() can do > sin(1:4) [1] 0.8414710 0.9092974 0.1411200 -0.7568025 kind regards, -- Jonas Stein <n...@jonasstein.de> ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.