r - trouble with converting values to scientific notation 1e-3 from 0.001 etc -
i know silly question think things should more clear on this. mean converting numbers normal scientific format example usual 0.001 1e-3 format.
i know there similar questions point want process without changing global options(scipen). not use first change options(scipen) in case other values dont want them changed.
here mean, lets have data 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 trial 1
when 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 can see time v column changed not when plotting
qplot(data=data_rep,x=v,y=value) trial 2
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 can see, value column changed original format. how can keep scientific notation in value column?
all of established wisdom on seems fall 2 camps. 1 camp says use scipen in options, don't reasons described.
the other solution leave scientific notation on , replace tick mark labels when go plot graph.
you can use format scientific=f create non-scientific tick mark labels. guide shows how customize labels different ggplot2 plots:
http://www.sthda.com/english/wiki/ggplot2-axis-ticks-a-guide-to-customize-tick-marks-and-labels

Comments
Post a Comment