[R] 'matrix' returns integer instead of decimal
Marc Schwartz
marc_schwartz at comcast.net
Mon Jan 21 04:25:54 CET 2008
Brant Inman wrote:
> R-helpers:
>
> I am experiencing some odd behavior with the 'matrix' function that I have
> not experienced before and was wondering if there is something that I was
> missing in my code.
>
> ---------------------------------
> > sessionInfo()
> R version 2.6.1 (2007-11-26)
> i386-pc-mingw32
> locale:
> LC_COLLATE=English_United States.1252;LC_CTYPE=English_United
> States.1252;LC_MONETARY=English_United
> States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252
> attached base packages:
> [1] splines grid stats graphics grDevices utils datasets
> methods
> [9] base
> other attached packages:
> [1] meta_0.8-2 car_1.2-7 arm_1.1-1 R2WinBUGS_2.1-6
>
> [5] coda_0.13-1 lme4_0.99875-9 Matrix_0.999375-3 nlme_3.1-86
>
> [9] latticeExtra_0.3-1 RColorBrewer_1.0-2 survival_2.34 MASS_7.2-39
>
> [13] foreign_0.8-23 RGraphics_1.0-6 lattice_0.17-4
> loaded via a namespace (and not attached):
> [1] rcompgen_0.1-17 tools_2.6.1
>
> > rm(list=ls())
>
>> matrix(c(7,47,4,38,20,96,1,14,10,48,2,101,12,161, # Make a data matrix
> + 1,28,1,19,22,49,25,162,31,200,9,39,22,193,
> + 0.5,45.5,31,131,4,75,31,220,7,55,3,91,14.5,
> + 25.5,3,65,25,54,24,41,37,95,11,17,26,49,13,
> + 84,38,170,29,60,9,20,44,47,30,160,40,185,10,
> + 41,40,185,4.5,46.5,60,140,12,75,42,225,26,57,
> + 17,92,23.5,23.5,6,68), ncol=2, byrow=T)
>
> [,1] [,2]
> [1,] 7.0 47
> [2,] 4.0 38
> [3,] 20.0 96
> [4,] 1.0 14
> [5,] 10.0 48
> [6,] 2.0 101
> [7,] 12.0 161
> [8,] 1.0 28
> [9,] 1.0 19
> [10,] 22.0 49
> [11,] 25.0 162
> [12,] 31.0 200
> [13,] 9.0 39
> [14,] 22.0 193
> [15,] 0.5 46
> [16,] 31.0 131
> [17,] 4.0 75
> [18,] 31.0 220
> [19,] 7.0 55
> [20,] 3.0 91
> [21,] 14.5 26
> [22,] 3.0 65
> [23,] 25.0 54
> [24,] 24.0 41
> [25,] 37.0 95
> [26,] 11.0 17
> [27,] 26.0 49
> [28,] 13.0 84
> [29,] 38.0 170
> [30,] 29.0 60
> [31,] 9.0 20
> [32,] 44.0 47
> [33,] 30.0 160
> [34,] 40.0 185
> [35,] 10.0 41
> [36,] 40.0 185
> [37,] 4.5 46
> [38,] 60.0 140
> [39,] 12.0 75
> [40,] 42.0 225
> [41,] 26.0 57
> [42,] 17.0 92
> [43,] 23.5 24
> [44,] 6.0 68
>
> ------------------------------------
>
> Note the problems in rows 15, 21, 37 and 43. They should read [0.5, 45.5],
> [14.5, 21.5], etc... The matrix function seems to be rounding the second
> column up to the next integer. Why would this occur? Can I do something to
> prevent this?
>
> I would very much appreciate any comments.
>
> Thanks,
>
> Brant Inman
> Mayo Clinic
Hi Brant,
I don't get this with 2.6.1 patched on Fedora 8:
mat <- matrix(c(7,47,4,38,20,96,1,14,10,48,2,101,12,161,
1,28,1,19,22,49,25,162,31,200,9,39,22,193,
0.5,45.5,31,131,4,75,31,220,7,55,3,91,14.5,
25.5,3,65,25,54,24,41,37,95,11,17,26,49,13,
84,38,170,29,60,9,20,44,47,30,160,40,185,10,
41,40,185,4.5,46.5,60,140,12,75,42,225,26,57,
17,92,23.5,23.5,6,68), ncol=2, byrow=T)
> mat
[,1] [,2]
[1,] 7.0 47.0
[2,] 4.0 38.0
[3,] 20.0 96.0
[4,] 1.0 14.0
[5,] 10.0 48.0
[6,] 2.0 101.0
[7,] 12.0 161.0
[8,] 1.0 28.0
[9,] 1.0 19.0
[10,] 22.0 49.0
[11,] 25.0 162.0
[12,] 31.0 200.0
[13,] 9.0 39.0
[14,] 22.0 193.0
[15,] 0.5 45.5
[16,] 31.0 131.0
[17,] 4.0 75.0
[18,] 31.0 220.0
[19,] 7.0 55.0
[20,] 3.0 91.0
[21,] 14.5 25.5
[22,] 3.0 65.0
[23,] 25.0 54.0
[24,] 24.0 41.0
[25,] 37.0 95.0
[26,] 11.0 17.0
[27,] 26.0 49.0
[28,] 13.0 84.0
[29,] 38.0 170.0
[30,] 29.0 60.0
[31,] 9.0 20.0
[32,] 44.0 47.0
[33,] 30.0 160.0
[34,] 40.0 185.0
[35,] 10.0 41.0
[36,] 40.0 185.0
[37,] 4.5 46.5
[38,] 60.0 140.0
[39,] 12.0 75.0
[40,] 42.0 225.0
[41,] 26.0 57.0
[42,] 17.0 92.0
[43,] 23.5 23.5
[44,] 6.0 68.0
Given some of the packages you have listed above, I would try to go into
R using:
R --vanilla
from a command line and then try your example again. It seems possible
that one of the packages has modified the behavior of print() on a
matrix or is causing some type of conflict.
For example, if options("digits") has been modified from it's default of
'7':
> options("digits")
$digits
[1] 7
to either 1 or 2, I can replicate the behavior you see above:
options(digits = 2)
> mat
[,1] [,2]
[1,] 7.0 47
[2,] 4.0 38
[3,] 20.0 96
[4,] 1.0 14
[5,] 10.0 48
[6,] 2.0 101
[7,] 12.0 161
[8,] 1.0 28
[9,] 1.0 19
[10,] 22.0 49
[11,] 25.0 162
[12,] 31.0 200
[13,] 9.0 39
[14,] 22.0 193
[15,] 0.5 46
[16,] 31.0 131
[17,] 4.0 75
[18,] 31.0 220
[19,] 7.0 55
[20,] 3.0 91
[21,] 14.5 26
[22,] 3.0 65
[23,] 25.0 54
[24,] 24.0 41
[25,] 37.0 95
[26,] 11.0 17
[27,] 26.0 49
[28,] 13.0 84
[29,] 38.0 170
[30,] 29.0 60
[31,] 9.0 20
[32,] 44.0 47
[33,] 30.0 160
[34,] 40.0 185
[35,] 10.0 41
[36,] 40.0 185
[37,] 4.5 46
[38,] 60.0 140
[39,] 12.0 75
[40,] 42.0 225
[41,] 26.0 57
[42,] 17.0 92
[43,] 23.5 24
[44,] 6.0 68
HTH,
Marc Schwartz
More information about the R-help
mailing list