numeric format

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

numeric format

cvandy
Hi!
I'm an R newbie and this should be a trivial problem, but I can't make it work and cannot find what I'm doing wrong in the literature.
I entered the the command:
table<-data.frame(x, scientific=F, digits=4)
table
This prints a column of x with 16 useless decimal places after the decimal point.  Also, it prints an unwanted index number (1-20) in the left column.
How do I get rid of the index column and how do I control the number of decimal places?
Thanks in advance.
CHV  
Reply | Threaded
Open this post in threaded view
|

Re: numeric format

jholtman
Those are parameter to 'print';  what you want is something like:

> x <- data.frame(a=runif(10))
> print(x)
             a
1  0.713705394
2  0.715496609
3  0.629578524
4  0.184360667
5  0.456639418
6  0.008667156
7  0.260985437
8  0.270915631
9  0.689128652
10 0.302484280
> print(x,scientific=F, digits=4)
          a
1  0.713705
2  0.715497
3  0.629579
4  0.184361
5  0.456639
6  0.008667
7  0.260985
8  0.270916
9  0.689129
10 0.302484
>


On 2/26/08, cvandy <[hidden email]> wrote:

>
> Hi!
> I'm an R newbie and this should be a trivial problem, but I can't make it
> work and cannot find what I'm doing wrong in the literature.
> I entered the the command:
> table<-data.frame(x, scientific=F, digits=4)
> table
> This prints a column of x with 16 useless decimal places after the decimal
> point.  Also, it prints an unwanted index number (1-20) in the left column.
> How do I get rid of the index column and how do I control the number of
> decimal places?
> Thanks in advance.
> CHV
> --
> View this message in context: http://www.nabble.com/numeric-format-tp15700452p15700452.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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 you are trying to solve?  Tell me what you want to
do, not how you want to do it.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: numeric format

Erik Iverson
In reply to this post by cvandy
Without knowing what your 'x' is, it's hard to see what is happening
that you don't like.

Your data.frame function creates a data.frame containing columns
scientific and digits, equal to FALSE and 4 for all rows, respectively.
  Is that what you want?


cvandy wrote:

> Hi!
> I'm an R newbie and this should be a trivial problem, but I can't make it
> work and cannot find what I'm doing wrong in the literature.
> I entered the the command:
> table<-data.frame(x, scientific=F, digits=4)
> table
> This prints a column of x with 16 useless decimal places after the decimal
> point.  Also, it prints an unwanted index number (1-20) in the left column.
> How do I get rid of the index column and how do I control the number of
> decimal places?
> Thanks in advance.
> CHV

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: numeric format

John Kane-2
In reply to this post by cvandy
Can you give a working example of what is happening
and explain what is x?

With a simple x vector of x <- rnorm(20, 5, 2)
I don't get anything like what you seem to be getting.

My code
===================================================
x <- rnorm(20, 5, 2)
table<-data.frame(x, scientific=F, digits=4)
table  
===================================================

The numbers on the left are simply line numbers that
are automatically printed when you are printing a
dataframe to the screen.  I don't see any way to
supress them for a simple command such as your
table

You might want to have a look at print and
print.default to address the digits problem

By the way, table is a reserved word in R and probably
should not be used as a name for a data.frame.

--- cvandy <[hidden email]> wrote:

>
> Hi!
> I'm an R newbie and this should be a trivial
> problem, but I can't make it
> work and cannot find what I'm doing wrong in the
> literature.
> I entered the the command:
> table<-data.frame(x, scientific=F, digits=4)
> table
> This prints a column of x with 16 useless decimal
> places after the decimal
> point.  Also, it prints an unwanted index number
> (1-20) in the left column.
> How do I get rid of the index column and how do I
> control the number of
> decimal places?
> Thanks in advance.
> CHV  
> --
> View this message in context:
>
http://www.nabble.com/numeric-format-tp15700452p15700452.html

> Sent from the R help mailing list archive at
> Nabble.com.
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: numeric format

Rolf Turner

On 27/02/2008, at 11:01 AM, John Kane wrote:

> Can you give a working example of what is happening
> and explain what is x?
>
> With a simple x vector of x <- rnorm(20, 5, 2)
> I don't get anything like what you seem to be getting.
>
> My code
> ===================================================
> x <- rnorm(20, 5, 2)
> table<-data.frame(x, scientific=F, digits=4)
> table
> ===================================================
>
> The numbers on the left are simply line numbers that
> are automatically printed when you are printing a
> dataframe to the screen.  I don't see any way to
> supress them for a simple command such as your
> table
>
> You might want to have a look at print and
> print.default to address the digits problem

        <snip>

I have often wanted to suppress these row numbers and for that purpose
wrote the following version of print.data.frame() which I keep in a
local package (that I have set up to be loaded automatically on startup)
which masks the system version of print.data.frame():

print.data.frame <- function (x, ..., digits = NULL, quote = FALSE,
     right = TRUE, srn = FALSE)
{
     if (length(x) == 0) {
         cat("NULL data frame with", length(row.names(x)), "rows\n")
     }
     else if (length(row.names(x)) == 0) {
         print.default(names(x), quote = FALSE)
         cat("<0 rows> (or 0-length row.names)\n")
     }
     else {
         if (!is.null(digits)) {
             op <- options(digits = digits)
             on.exit(options(op))
         }
         rowlab <- if (srn)
             rep("", nrow(x))
         else row.names(x)
         prmatrix(format(x), rowlab = rowlab, ..., quote = quote,
             right = right)
     }
     invisible(x)
}

The ``srn'' argument means ``suppress row numbers''; it defaults to  
FALSE so by default
the behaviour of my print.data.frame() is the same as that of the  
system print.data.frame().
To suppress the row numbers you can say, e.g.

        print(junk,srn=TRUE)

Note to Young Players --- you need only type ``print'' in the above,  
r.t. ``print.data.frame'',
since print.data.frame() is a ``method'' for print().

I once suggested to an R Core person that my version of  
print.data.frame() be adopted as the
system version, but was politely declined.

                        cheers,

                                Rolf Turner


######################################################################
Attention:\ This e-mail message is privileged and confid...{{dropped:9}}

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: numeric format

cvandy
In reply to this post by Erik Iverson
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@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.


Reply | Threaded
Open this post in threaded view
|

Re: numeric format

Don MacQueen
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)
>  head(tbl)
   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')
>  head(tbl)
   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
>
>______________________________________________
>[hidden email] 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.
>
>
>
>--
>View this message in context:
>http://www.nabble.com/numeric-format-tp15700452p15702792.html
>Sent from the R help mailing list archive at Nabble.com.
>
>______________________________________________
>[hidden email] 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.


--
---------------------------------
Don MacQueen
Lawrence Livermore National Laboratory
Livermore, CA, USA
925-423-1062
[hidden email]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: numeric format

Mike Prager
In reply to this post by Rolf Turner
Rolf Turner <[hidden email]> wrote:

> I have often wanted to suppress these row numbers and for that purpose
> wrote the following version of print.data.frame()
[...]
> The ``srn'' argument means ``suppress row numbers'';
[...]
> I once suggested to an R Core person that my version of  
> print.data.frame() be adopted as the
> system version, but was politely declined.

Rolf--

Clearly, and appropriately, R development is not a democratic
process. Still, if a vote were held, I would support your
version.  I have also needed to suppress row names from time to
time.

--
Mike Prager, NOAA, Beaufort, NC
* Opinions expressed are personal and not represented otherwise.
* Any use of tradenames does not constitute a NOAA endorsement.

______________________________________________
[hidden email] 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.