0

I have list of data.frame, where I need to do transformation for .score column. However, I implemented helper function for this transformation. After I call .helperFunc for my input list of data.frame, but I got weird pvalue format in first, third data.frame. How to normalize rather big decimal to simple scientific number ? Can anyone tell me how to make this happen easily ?

toy data :

savedDF <- list(
  bar = data.frame(.start=c(12,21,37), .stop=c(14,29,45), .score=c(5,69,14)),
  cat = data.frame(.start=c(18,42,18,42,81), .stop=c(27,46,27,46,114), .score=c(15,5,15,5,134)),
  foo = data.frame(.start=c(3,3,33,3,33,91), .stop=c(26,26,42,26,42,107), .score=c(22,22,6,22,6,7))
)

I got this weird output:

> .savedDF
$bar
  .start .stop .score                                                                 p.value
1     12    14      5 0.000010000000000000000817488438054070343241619411855936050415039062500
2     21    29     69 0.000000000000000000000000000000000000000000000000000000000000000000001
3     37    45     14 0.000000000000009999999999999999990459020882127560980734415352344512939

$cat
  .start .stop .score p.value
1     18    27     15   1e-15
2     42    46      5   1e-05
3     18    27     15   1e-15
4     42    46      5   1e-05
5     81   114    134  1e-134

$foo
  .start .stop .score                  p.value
1      3    26     22 0.0000000000000000000001
2      3    26     22 0.0000000000000000000001
3     33    42      6 0.0000010000000000000000
4      3    26     22 0.0000000000000000000001
5     33    42      6 0.0000010000000000000000
6     91   107      7 0.0000001000000000000000

I don't know what happen this, only second data.frame' format is desired. How can I normalize p.value column as simple as possible ?

last column of cat is considered to be desired format, or more precise but simple scientific number is also fit for me.

How can I make this normalization for unexpectedly long decimal numbers ? How can I achieve my desired output ? Any idea ? Thanks a lot

Hamilton
  • 620
  • 2
  • 14
  • 32
  • 3
    You should not change the value in the data. Rather, you should depend on your printing options to display the value in scientific notation. What does `getOption(x = "scipen")` return? – Gregor Thomas Nov 22 '16 at 00:20
  • Yep, I don't get anything like your result with an unchanged version of R. I get nice scientific values like 1e-05 1e-69 etc for everything. – thelatemail Nov 22 '16 at 00:21
  • @Jerry.Shad - basically, I can't replicate what you are showing by running your code. I get values like shown for `.savedDF$cat` for every part of the list. I suggest you take a look at the option that Gregor suggested, as you've most likely edited a setting somewhere which is causing the issue. – thelatemail Nov 22 '16 at 00:30
  • 1
    If you enter the code `getOption(x = "scipen")` into your R console, what do you get? – Gregor Thomas Nov 22 '16 at 00:36
  • 1
    Okay. Run `options(scipen = 0)` and your problem should be solved. – Gregor Thomas Nov 22 '16 at 00:37
  • @Gregor wow, this trick works. So in my function, I could add this option as well ? – Hamilton Nov 22 '16 at 00:39

1 Answers1

2

0 is the default scipen option. (See ?options for more details.) You apparently have changed the option to 100, which tells R to use decimal notation unless it is 100 characters longer than scientific notation. To get back to the default, run the line

options(scipen = 0)

As to "So in my function, I could add this option as well?" - you shouldn't do that. Doing it in your script is fine, but not in a function. Functions really shouldn't set user options. That's likely how you got in to this mess - some function you used probably rudely ran options(scipen = 100) and changed your options without you being aware.

Related: the opposite question How to disable scientific notation in R?

Community
  • 1
  • 1
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294