1

I'm working with a data table which has a column of results which begin as character values (chr). Because I need to do mathematical calculations with some of these values I copy the original column, but change the values which are non-numerics to specific numeric values. For example I use -999 for a result value of "*Not reported" and set up similar substitute values for the non-numeric characters. Other collected values are numbers such as 7.3 or 238. Result values vary based on the instruments used in collection and the characteristic being reported.

Here's a small sample of the data table dt (other columns excluded).

RESULT_VALUE    RESULT_TRANSLATED
43.2               43.2000
*Not reported      -999.00000
0.19               0.19000
0.058              0.05800

The RESULT_TRANSLATED values are converted using dt$RESULT_TRANSLATED <- as.numeric(as.character(dt$RESULT_VALUE)).

Option values for scipen and digits are 0 and 7 respectively.

The scipen was set to 0 to force a column of location identifiers (separate column not shown above) to display as non-scientific numbers (e.g. 23434 vs 2.3434e04). Digits was set to 7 to enable display of recorded field data without truncating values or decreasing their significant figures.

What I'd like to see is a result more along these lines for the conversion:

RESULT_VALUE    RESULT_TRANSLATED
43.2               43.2
*Not reported      -999
0.19               0.19
0.058              0.058

Here, the translated data reflects the significant figures for the results versus adding accuracy that doesn't exist. So values like 0.058 don't imply a measurement of 0.05800 or 0.19 doesn't imply 0.19000 .

Unfortunately it's appearing I cannot have my cake (the location identifiers without sci-notation) and eat it too (have proper accuracy/sig figs in converted results values). While I could ignore the location identification scientific notation (possibly changing it to a chr vs a numeric column), changing the accuracy of the measurements is something I need to avoid.

Perhaps someone can enlighten me as to whether this is possible, particularly when the result set includes results reported from instruments with varying accuracy. I've searched through the various resources and found some related topics, but nothing that quite addresses the issue.

Thanks for your kind assistance/direction.

NWdev
  • 483
  • 1
  • 6
  • 19
  • 3
    Won't happen unless you format them as characters. Having 0.058 in the column will force three decimal places in all the other elements for numeric vectors – Rich Scriven Mar 25 '15 at 03:00
  • 1
    @RichardScriven is right. Also, if you're performng numeric calculations, won't it be better to use `NA` instead of -999? – A5C1D2H2I1M1N2O1R2T1 Mar 25 '15 at 03:02

0 Answers0