[R] Formatting numeric values in a data frame

Philipp Pagel p.pagel at wzw.tum.de
Wed Feb 25 22:36:59 CET 2009


On Wed, Feb 25, 2009 at 01:19:36PM -0800, Pele wrote:
> 
> Hi R users,
> 
> I have a data frame that contains 10K obs and 200 variables 
> where I am trying to format the numeric columns to look
> like the output table below (format to 2 decimal places) but I am 
> having no luck.. Can someone tell me the best way to 
> accomplist this? 

format() will do tht for you:

> foo <- data.frame(a=rnorm(10), b=rnorm(10))
> format(foo, digits=1)
a     b
1  -0.12  1.28
2  -0.21 -0.46
3   0.09  0.45
4  -0.58 -0.88
5  -0.04 -0.20
6   0.03 -0.43
7   0.65 -1.53
8   0.39  0.81
9   0.69  0.09
10 -0.61 -1.15


Note that here we got 2 instead of the requested 1 significant digit
because that would have set some values to 0.0. More in the manual
page.

cu
	Philipp

-- 
Dr. Philipp Pagel
Lehrstuhl für Genomorientierte Bioinformatik
Technische Universität München
Wissenschaftszentrum Weihenstephan
85350 Freising, Germany
http://mips.gsf.de/staff/pagel




More information about the R-help mailing list