# Re: [R] numeric format

From: Don MacQueen <macq_at_llnl.gov>
Date: Tue, 26 Feb 2008 22:06:15 -0800

While we're at it, and since you're new...

Your example will be much easier for r-help folks to read if you do it like this:

j <- 0.4
for(i in 1:20) {

j=j+0.1
cp[i] <- pnorm(-j*3)*10^6
ratio[i] <- j
}

table <- data.frame(ratio,cp)
table

But the loop is unnecessary. Try

j <- seq(0.5, by=0.1, length=20)
tbl <- data.frame(ratio=j, cp= pnorm(-3*j)*10^6)

Also, the formatting you are getting is not, I don't think, the default. I get:

> j <- seq(0.5, by=0.1, length=20)
> tbl <- data.frame(ratio=j, cp= pnorm(-3*j)*10^6)

ratio cp

```1   0.5 66807.201
2   0.6 35930.319
3   0.7 17864.421
4   0.8  8197.536
5   0.9  3466.974
6   1.0  1349.898

```

Note that they are aligned to all have 3 places after the decimal point.

Do you want the actual cp values to be adjusted to have some other number of decimal places, or do you just want the format changed?

For the former, use, for example, round(cp,1).

For the latter, it kind of depends on your ultimate goal. If you want to control the number of decimal places, because you're going to move it into a report, for example, you might try looking into the formatC() and use something like

formatC(tbl\$cp,digits=1,format='f')

As in,

> tbl\$cp <- formatC(tbl\$cp,digits=1,format='f')

ratio cp

```1   0.5 66807.2
2   0.6 35930.3
3   0.7 17864.4
4   0.8  8197.5
5   0.9  3467.0
6   1.0  1349.9

```

To get rid of the row labels, you will have to use Rolf's suggestion, or perhaps delve into the write.table function.

-Don

At 3:41 PM -0800 2/26/08, cvandy wrote:
>Thanks, Erik,
>This is a partial copy of my code. I want to get rid of the left column of
>integers and I want to control the number of decimal places after the
>decimal point.
>
> > j<-0.4
>> for(i in 1:20){
>+ j=j+0.1;cp[i]<-pnorm(-j*3)*10^6;ratio[i]<-j}
> > table<-data.frame(ratio,cp)
>> table
> ratio cp
>1 0.5 66807.201268858
>2 0.6 35930.3191129258
>3 0.7 17864.4205628166
>4 0.8 8197.53592459613
>5 0.9 3466.97380304067
>6
>
>> CHV
>
>______________________________________________
>R-help_at_r-project.org mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>and provide commented, minimal, self-contained, reproducible code.
>
>
>
>--
>View this message in context:
>http://www.nabble.com/numeric-format-tp15700452p15702792.html
>Sent from the R help mailing list archive at Nabble.com.
>
>______________________________________________
>R-help_at_r-project.org mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>and provide commented, minimal, self-contained, reproducible code.

```--
---------------------------------
Don MacQueen
Lawrence Livermore National Laboratory
Livermore, CA, USA
925-423-1062
macq_at_llnl.gov

______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help