Jerry.Shad - 3 months ago 22

R Question

I have list of data.frame, where I need to do transformation for

`.score`

`.helperFunc`

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

)

so I implement helper function to convert p-value as follows :

`.helperFunc <- function(obj, pvalueBase=1L, ...) {`

# input param check

if(is.null(obj$p.value)) {

p.value <- 10^(obj$.score/(- pvalueBase))

obj$p.value <- p.value

} else {

stop("error, missing pvalue")

}

return(obj)

}

.savedDF <- lapply(savedDF, .helperFunc)

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`

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

Answer

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?