0

I have a matrix and I would like to subset from a specific cell. The matrix contains a mix of numbers, some in scientific notation.

The first issue, is that I would like to remove the scientific notation.

The second issue is that when I attempt to subset the number, it rounds to only 1 decimal place. However, I would like to keep 2 decimal places. Incidently, it only does this for numbers that when rounded would end in 0.

I have already looked at R rounds decimal values in matrix when subsetting and a few other SO posts on formatting matrix output using format() and options("scipen" = 999). None of these things worked for me.

Here is a minimal reproducible example:

# Create matrix
x <- c(3.3616295, 1.085965, 2.425796e+06, 2.049605e+180, 1.000000, 2.8585595, 3.953233)
y <- c(0.7991626, 1.217628, 3.202742e-01, 1.496401e+173, 1.291188, 0.5426498, 1.176930)
mx1 <- cbind(x, y)

# Round to 2 decimal places
mx2 <- round(mx1, 2)

mx2
             x             y
[1,]  3.360000e+00  8.000000e-01
[2,]  1.090000e+00  1.220000e+00
[3,]  2.425796e+06  3.200000e-01
[4,] 2.049605e+180 1.496401e+173
[5,]  1.000000e+00  1.290000e+00
[6,]  2.860000e+00  5.400000e-01
[7,]  3.950000e+00  1.180000e+00

# Extract value from row 1, col 2
mx2[1, 2]

 y 
0.8 

# The above value was rounded to one decimal place, not 2.
# Try using format()
mx3 <- format(mx2, digits = 2, scientific = FALSE)
mx3
     x          y         
[1,] " 3.4e+00" " 8.0e-01"
[2,] " 1.1e+00" " 1.2e+00"
[3,] " 2.4e+06" " 3.2e-01"
[4,] "2.0e+180" "1.5e+173"
[5,] " 1.0e+00" " 1.3e+00"
[6,] " 2.9e+00" " 5.4e-01"
[7,] " 4.0e+00" " 1.2e+00"

This is still not what I want. The scientific notation should be gone and when I extract my value it should be 0.80.

Any ideas?

Community
  • 1
  • 1
RNB
  • 457
  • 1
  • 5
  • 14
  • 2
    0.8 and 0.80 are exactly the same. Trailing zeros are useless. If you want to keep 2 places after decimal point try to convert numbers to text and add padding. Now, for you to be able to remove scientific notation, please be aware that quantities should be displayable in screen (and about in the same orders if magnitude): you have a number with 180 zeros! (millions^30), so it's unrealistic to expect it to drop scientific notation. I think R is behaving as it should. – PavoDive Feb 04 '16 at 12:50
  • Maybe I should clarify a bit. I do realize that 0.8 and 0.80 are exactly the same number. The reason I want it as 0.80, is because i'm extracting this data to put into a table for publication. All numbers need to be formatted the same way. Row 4 isn't necessary for my output, so do you think format() would work if I dropped that row? – RNB Feb 04 '16 at 13:20
  • You are mixing up numbers (which is what is stored in your matrix) with their character representation. There are no numbers in *scientific notation* or *rounded to 1 decimal * place (in the sense that the digits after the first decimal place are absent) in your matrix. It just contains numbers. Whether you have one or two digits and whether you see scientific notation or not is something that is decided when the numbers are converted to character when you print them. – Stibu Feb 04 '16 at 13:39
  • 1
    `round(mx2[-4, ], 2)` and `round(mx2[-(3:4), ], 2)` round to 2 digits. if at least one of the cells in each column has a hundredths digit, then you won't need to use format which is the case here. r simplifies printing to the screen so if all the values in a column have a trailing 0, it would be dropped in that case and format would be needed – rawr Feb 04 '16 at 13:39
  • If you just want to format output, see: http://stackoverflow.com/questions/3443687/formatting-decimal-places-in-r – arvi1000 Feb 04 '16 at 15:56

0 Answers0