Reputation: 4635
I know that this could be silly question but I think things should be more clear about on this. I mean converting numbers from normal to scientific format for example from usual 0.001 to 1e-3 format.
I know there are some similar questions but the point is I want to do this process without changing global options(scipen)
. It is not good use always first change options(scipen)
in my case as other values which I dont want them to be changed.
Here is what I mean, lets say we have a data like this
set.seed(12345)
set =rep(rep(c("1","2"),each=5),times=1)
V=rep(seq(1,1.4,0.1),times=2)
value <- replicate(1,c(replicate(2,sort(10^runif(5,-3,0),decreasing=FALSE))))
data_rep <- data.frame(V, value,set)
#> data_rep
# V value set
#1 1.0 0.002351715 1
#2 1.1 0.005382811 1
#3 1.2 0.023703932 1
#4 1.3 0.058314556 1
#5 1.4 0.476184589 1
#6 1.0 0.001595635 2
#7 1.1 0.001896175 2
#8 1.2 0.034667861 2
#9 1.3 0.097931107 2
#10 1.4 0.197982134 2
When we do options(scipen=-10)
#> data_rep
# V value set
#1 1.0e+00 2.341224e-02 1
#2 1.1e+00 1.454493e-01 1
#3 1.2e+00 1.918435e-01 1
#4 1.3e+00 4.239548e-01 1
#5 1.4e+00 4.553797e-01 1
#6 1.0e+00 3.155843e-03 2
#7 1.1e+00 9.446831e-03 2
#8 1.2e+00 3.370335e-02 2
#9 1.3e+00 1.524459e-01 2
#10 1.4e+00 9.315600e-01 2
As we can see this time V column also changed which is not good when plotting
qplot(data=data_rep,x=V,y=value)
changing format using format
function
options(scipen = 0)
library(dplyr)
df_format <- data_rep%>%
mutate(value=as.numeric(format(value,scientific=TRUE,digits=4))) %>%
mutate(logic=ifelse(value<2e-1,TRUE,FALSE))
# > df_format
# V value set logic
#1 1.0 0.023410 1 TRUE
#2 1.1 0.145400 1 TRUE
#3 1.2 0.191800 1 TRUE
#4 1.3 0.424000 1 FALSE
#5 1.4 0.455400 1 FALSE
#6 1.0 0.003156 2 TRUE
#7 1.1 0.009447 2 TRUE
#8 1.2 0.033700 2 TRUE
#9 1.3 0.152400 2 TRUE
#10 1.4 0.931600 2 FALSE
As we can see, value column changed to the original format. how can we keep scientific notation in the value
column?
Upvotes: 1
Views: 1483
Reputation: 23214
All of the established wisdom on this seems to fall into 2 camps. One camp says to use scipen
in options
, which you don't like for the reasons you described.
The other solution is to leave scientific notation on and replace the tick mark labels when you go to plot the graph.
You can use format
with scientific=F
to create the non-scientific tick mark labels. This guide shows how to customize those labels for different ggplot2
plots:
http://www.sthda.com/english/wiki/ggplot2-axis-ticks-a-guide-to-customize-tick-marks-and-labels
Upvotes: 2