[R] How to convert data to 'normal' if they are in the form of standard scientific notations?

arun smartpink111 at yahoo.com
Tue Aug 7 18:09:37 CEST 2012



Hi, 

Try these also:

 a<-c(2.0e+9,2.1e+9) 
 is.numeric(a)
[1] TRUE
formatC(a,format="fg")
#[1] "2000000000" "2100000000"
 formatC(a,format="f")
#[1] "2000000000.0000" "2100000000.0000"

 formatC(a,format="f",digits=0)
#[1] "2000000000" "2100000000"
A.K.


----- Original Message -----
From: HJ YAN <yhj204 at googlemail.com>
To: Jean V Adams <jvadams at usgs.gov>
Cc: r-help at r-project.org
Sent: Monday, August 6, 2012 7:05 PM
Subject: Re: [R] How to convert data to 'normal' if they are in the form of standard scientific notations?

Dear Jean

Thanks a lot for your help.

The reason I did not provide producible code is that my work started with
reading in some large csv files, e.g. the data is not created by myself.
But the data is from the same data provider so I would expect to receive
data in exactly same data format.


I use "read.csv" to read the data in. My major curious is that by using
exactly same code as I provided in my email, e.g. 'as.factor' why one of
them work (e.g. convert the numerical data to factor) but the other  one
remains numerical with scientific notation?  So, in R, how do I check if
the data format are different for these two files in their original csv
files, which  might cause the different results..?

Also I tried your code and created some reproducible examples, but still
can not make it work as in your example....


> a<-c(2.0e+9,2.1e+9)> print(a,digits=4)[1] 2000000000 2100000000  # I expected to see 2.0e+9 here...?> print(a,digits=7)[1] 2000000000 2100000000  # Think here I should expect same 2.0e+9?> getOption("digits")      # Checking my default number of digits now..[1] 7> b<-c(30000000,31000000)> print(b)[1] 30000000 31000000       # This is what I expected to see> print(b,digits=5)[1] 30000000 31000000   # I'm so confused why it is not working, e.g. printing 3.0e+9!> getOption("digits")   # checking again, but now I would expect it has being changed to 5[1] 7


Any thoughts please...?

Thanks
HJ


On Mon, Aug 6, 2012 at 7:04 PM, Jean V Adams <jvadams at usgs.gov> wrote:

> HJ,
>
> You don't provide any reproducible code, so I had to make up my own.
>
> dat <- data.frame(a=letters[1:5], x=c(20110911001084, 20110911001084,
>         20110911001084, 20110911001084, 20110911001084),
>         y=c(2.10004e+12, 2.10004e+12, 2.10004e+12, 2.10004e+12,
> 2.10004e+12))
>
> In my example, the long numbers print out without scientific notation.
>
> dat
>   a              x             y
> 1 a 20110911001084 2100040000000
> 2 b 20110911001084 2100040000000
> 3 c 20110911001084 2100040000000
> 4 d 20110911001084 2100040000000
> 5 e 20110911001084 2100040000000
>
> I can make it print with scientific notation using the digits argument to
> the print() function.
>
> print(dat, digits=3)
>   a        x       y
> 1 a 2.01e+13 2.1e+12
> 2 b 2.01e+13 2.1e+12
> 3 c 2.01e+13 2.1e+12
> 4 d 2.01e+13 2.1e+12
> 5 e 2.01e+13 2.1e+12
>
> What is your default number of digits?
> getOption("digits")
>
> Jean
>
>
> HJ YAN <yhj204 at googlemail.com> wrote on 08/06/2012 11:14:17 AM:
>
> >
> > Dear R users
> >
> > I read two csv data files into R and  called them Tem1 and Tem5.
> >
> > For the first column, data in Tem1 has 13 digits where in Tem5 there are
> 14
> > digits for each observation.
> >
> > Originally there are 'numerical' as can be seen in my code below.  But
> how
> > can I display/convert them using other form rather than scientific
> > notations which seems a standard/default?
> >
> >  I want them to be in the form like '20110911001084', but I'm very
> confused
> > why when I used 'as.factor' call it works for my 'Tem1' but not for
> > 'Tem5'...??
> >
> >
> > Many thanks!
> >
> > HJ
> >
> > > Tem1[1:5,1][1] 2.10004e+12 2.10004e+12 2.10004e+12 2.10004e+12 2.
> > 10004e+12> Tem5[1:5,1][1] 2.011091e+13 2.011091e+13 2.011091e+13 2.
>
> > 011091e+13 2.011091e+13> class(Tem1[1:5,1])[1] "numeric"> class(Tem5
> > [1:5,1])[1] "numeric"> as.factor(Tem1[1:5,1])[1] 2.10004e+12 2.
> > 10004e+12 2.10004e+12 2.10004e+12 2.10004e+12
> > Levels: 2.10004e+12> as.factor(Tem5[1:5,1])[1] 20110911001084
> > 20110911001084 20110911001084 20110911001084 20110911001084
> > Levels: 20110911001084
>

    [[alternative HTML version deleted]]

______________________________________________
R-help at 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.




More information about the R-help mailing list