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

Petr PIKAL petr.pikal at precheza.cz
Tue Aug 7 10:11:00 CEST 2012


Hi

> 
> 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)
is.numeric(a)
[1] TRUE
a.f<-factor(a)
is.numeric(a.f)
[1] FALSE
is.factor(a.f)
[1] TRUE

so factor comes correctly

print(a,digits=12)
[1] 2.0e+09 2.1e+09

print(a, digits=21)
[1] 2000000000.00000000000 2100000000.00000000000

b<-c(30000000,31000000)
print(b,digits=5)
[1] 3.0e+07 3.1e+07

So the printed result depends probably on your local setting.
See also ?options help page. And maybe also ?format and ?sprintf

Regards
Petr


> 
> 
> > 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