David Disabato
2018-Sep-27 16:35 UTC
[R] Printing standard notation and scientific notation in the same column of a dataframe
Hi R-help, I was wondering if it was possible for a column of a dataframe to print some numbers in standard notation and some in scientific notation. Say my column of data (i.e., dat$x) has numbers between 0 and 1 with a few numbers very close to 0. When using the "scipen" argument in "options," R seems to print all numbers of a column in scientific notation if one number in the column is a decimal with a starting digit smaller than the "scipen" argument. It is annoying that is changes ALL numbers in that column to scientific notation though. For example, I do want .00000000000000000001 in scientific notation, but I want .52 in standard form. Ideally, an example dataframe column would print as something like this: print(dat$x) .52 .17 .03 1.0e-20 However, I cannot figure out how to do this. Any solutions people are aware of? -- David J. Disabato, M.A. Clinical Psychology Doctoral Student George Mason University ddisabat at gmu.edu Email is not a secure form of communication as information and confidentiality cannot be guaranteed. Information provided in an email is not intended to be a professional service. In the case of a crisis or emergency situation, call 911. [[alternative HTML version deleted]]
David Winsemius
2018-Sep-27 18:53 UTC
[R] Printing standard notation and scientific notation in the same column of a dataframe
> On Sep 27, 2018, at 9:35 AM, David Disabato <ddisab01 at gmail.com> wrote: > > Hi R-help, > > I was wondering if it was possible for a column of a dataframe to print > some numbers in standard notation and some in scientific notation. Say my > column of data (i.e., dat$x) has numbers between 0 and 1 with a few numbers > very close to 0. When using the "scipen" argument in "options," R seems to > print all numbers of a column in scientific notation if one number in the > column is a decimal with a starting digit smaller than the "scipen" > argument. It is annoying that is changes ALL numbers in that column to > scientific notation though. For example, I do want .00000000000000000001 in > scientific notation, but I want .52 in standard form. Ideally, an example > dataframe column would print as something like this: > > print(dat$x) > .52 > .17 > .03 > 1.0e-20 > > However, I cannot figure out how to do this. Any solutions people are aware > of?Perhaps cat?> cat(x)0.52 0.17 0.03 1e-20> > -- > David J. Disabato, M.A. > Clinical Psychology Doctoral Student > George Mason University > ddisabat at gmu.edu > > Email is not a secure form of communication as information and > confidentiality cannot be guaranteed. Information provided in an email is > not intended to be a professional service. In the case of a crisis or > emergency situation, call 911. > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.David Winsemius Alameda, CA, USA 'Any technology distinguishable from magic is insufficiently advanced.' -Gehm's Corollary to Clarke's Third Law
MacQueen, Don
2018-Sep-27 21:51 UTC
[R] Printing standard notation and scientific notation in the same column of a dataframe
First compare> format(c(0.52, 0.17, 0.03, 1e-20))[1] "5.2e-01" "1.7e-01" "3.0e-02" "1.0e-20"> prettyNum(c(0.52, 0.17, 0.03, 1e-20))[1] "0.52" "0.17" "0.03" "1e-20">If you want to print one column at a time, that will do what you ask. If you want to print the entire data frame, with numeric columns formatting this way when needed, it will take more work. Start with ?print.data.frame, which says, in part, This calls 'format' which formats the data frame column-by-column, then converts to a character matrix and dispatches to the 'print' method for matrices. Fortunately the code for print.data.frame is fairly short and simple. Looking at it, it calls format.data.frame (also fairly short and simple), which in turn uses format(). So there does not appear to be a built in option for the formatting you want. Some possible approaches: 1) create your own version of format.data.frame which uses prettyNum on numeric columns and format on all other types. If it appears earlier in the path than base R's format.data.frame it might be used instead. 2) create your own versions of both print.data.frame and format.data.frame, again causing it to use prettyNum on numeric columns 3) manually convert your numeric columns to character columns using prettyNum, then print that. Alignment will probably change, which you may not want, but at least you'll get nicer to read numbers. (as an aside, I'll claim that this is an example of the power of open-source software -- if you don't like the defaults, one can make one's own version to work however is desired -- but it does take some work) -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 Lab cell 925-724-7509 ?On 9/27/18, 11:53 AM, "R-help on behalf of David Winsemius" <r-help-bounces at r-project.org on behalf of dwinsemius at comcast.net> wrote: > On Sep 27, 2018, at 9:35 AM, David Disabato <ddisab01 at gmail.com> wrote: > > Hi R-help, > > I was wondering if it was possible for a column of a dataframe to print > some numbers in standard notation and some in scientific notation. Say my > column of data (i.e., dat$x) has numbers between 0 and 1 with a few numbers > very close to 0. When using the "scipen" argument in "options," R seems to > print all numbers of a column in scientific notation if one number in the > column is a decimal with a starting digit smaller than the "scipen" > argument. It is annoying that is changes ALL numbers in that column to > scientific notation though. For example, I do want .00000000000000000001 in > scientific notation, but I want .52 in standard form. Ideally, an example > dataframe column would print as something like this: > > print(dat$x) > .52 > .17 > .03 > 1.0e-20 > > However, I cannot figure out how to do this. Any solutions people are aware > of? Perhaps cat? > cat(x) 0.52 0.17 0.03 1e-20 > > -- > David J. Disabato, M.A. > Clinical Psychology Doctoral Student > George Mason University > ddisabat at gmu.edu > > Email is not a secure form of communication as information and > confidentiality cannot be guaranteed. Information provided in an email is > not intended to be a professional service. In the case of a crisis or > emergency situation, call 911. > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. David Winsemius Alameda, CA, USA 'Any technology distinguishable from magic is insufficiently advanced.' -Gehm's Corollary to Clarke's Third Law ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
Duncan Mackay
2018-Sep-28 05:41 UTC
[R] Printing standard notation and scientific notation in the same column of a dataframe
Hi If you do not require the zeros to the right in the scientific notation x [1] 5.2e-01 1.7e-01 3.0e-02 1.0e-20 zapsmall(x) [1] 0.52 0.17 0.03 0.00 Then use format, formatC or sprintf for character conversions Regards Duncan Duncan Mackay Department of Agronomy and Soil Science University of New England Armidale NSW 2350 -----Original Message----- From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of David Winsemius Sent: Friday, 28 September 2018 04:53 To: David Disabato Cc: r-help at r-project.org Subject: Re: [R] Printing standard notation and scientific notation in the same column of a dataframe> On Sep 27, 2018, at 9:35 AM, David Disabato <ddisab01 at gmail.com> wrote: > > Hi R-help, > > I was wondering if it was possible for a column of a dataframe to print > some numbers in standard notation and some in scientific notation. Say my > column of data (i.e., dat$x) has numbers between 0 and 1 with a fewnumbers> very close to 0. When using the "scipen" argument in "options," R seems to > print all numbers of a column in scientific notation if one number in the > column is a decimal with a starting digit smaller than the "scipen" > argument. It is annoying that is changes ALL numbers in that column to > scientific notation though. For example, I do want .00000000000000000001in> scientific notation, but I want .52 in standard form. Ideally, an example > dataframe column would print as something like this: > > print(dat$x) > .52 > .17 > .03 > 1.0e-20 > > However, I cannot figure out how to do this. Any solutions people areaware> of?Perhaps cat?> cat(x)0.52 0.17 0.03 1e-20> > -- > David J. Disabato, M.A. > Clinical Psychology Doctoral Student > George Mason University > ddisabat at gmu.edu > > Email is not a secure form of communication as information and > confidentiality cannot be guaranteed. Information provided in an email is > not intended to be a professional service. In the case of a crisis or > emergency situation, call 911. > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.David Winsemius Alameda, CA, USA 'Any technology distinguishable from magic is insufficiently advanced.' -Gehm's Corollary to Clarke's Third Law ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.