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?