This might do what you want: > x <- read.table(textConnection("ID x1 > x2 x3 + a1 0.0123334 0.000020000 12.33338 + b3 0.477896366 0.000020000 9.44446 + c1 0.477896366 0.000020000 9.44446 + d5 0.477896366 0.000020000 9.44446 + e1 0.477896366 0.000020000 9.44446 + f2 0.477896366 0.000020000 9.44446 + g6 0.333333333 0.000020000 9.44446 + h8 0.333333333 0.000020000 9.44446 + i2 0.333333333 0.000020000 9.44446 + k2 0.333333333 0.000020000 9.44446"), header=TRUE) > closeAllConnections() > x.c <- x # make a copy > x.c[] <- lapply(x.c, function(.col){ + if (is.numeric(.col)) return(sprintf("%.2f", .col)) + else return(.col) + }) > > x.c ID x1 x2 x3 1 a1 0.01 0.00 12.33 2 b3 0.48 0.00 9.44 3 c1 0.48 0.00 9.44 4 d5 0.48 0.00 9.44 5 e1 0.48 0.00 9.44 6 f2 0.48 0.00 9.44 7 g6 0.33 0.00 9.44 8 h8 0.33 0.00 9.44 9 i2 0.33 0.00 9.44 10 k2 0.33 0.00 9.44 >
On Wed, Feb 25, 2009 at 4:19 PM, Pele <drdi...@yahoo.com> 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? > > Thanks in advance for any help! > > str(ad.test) > 'data.frame': 10,000 obs. of 200 variables: > $ ID : Factor w/ .................. > $ x1 : num 0.1123334 0.4778966.......... > $ x2 : num 0.00002 0.00002.............. > $ x3 : num 12.33338 9.44446............. > ==================================================== > Data.frame.... > ID x1 x2 > x3 > a1 0.0123334 0.000020000 > 12.33338 > b3 0.477896366 0.000020000 9.44446 > c1 0.477896366 0.000020000 9.44446 > d5 0.477896366 0.000020000 9.44446 > e1 0.477896366 0.000020000 9.44446 > f2 0.477896366 0.000020000 9.44446 > g6 0.333333333 0.000020000 9.44446 > h8 0.333333333 0.000020000 9.44446 > i2 0.333333333 0.000020000 9.44446 > k2 0.333333333 0.000020000 9.44446 > > ========================================================= > > Output: > > ID x1 x2 x3 > a1 0.01 0.00 12.33 > b3 0.48 0.00 9.44 > c1 0.48 0.00 9.44 > d5 0.48 0.00 9.44 > e1 0.48 0.00 9.44 > f2 0.48 0.00 9.44 > g6 0.33 0.00 9.44 > h8 0.33 0.00 9.44 > i2 0.33 0.00 9.44 > k2 0.33 0.00 9.44 > -- > View this message in context: > http://www.nabble.com/Formatting-numeric-values-in-a-data-frame-tp22212031p22212031.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. > -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? ______________________________________________ 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.