Guest User

Untitled

a guest
Oct 19th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. set.seed(1)
  2. m = array(runif(12) * 10, c(3, 4))
  3. class(m)
  4. typeof(m)
  5. dput(m)
  6. # > class(m)
  7. # [1] "matrix"
  8. # > typeof(m)
  9. # [1] "double"
  10. # > dput(m)
  11. # structure(c(2.655086631421, 3.7212389963679, 5.72853363351896,
  12. # 9.08207789994776, 2.01681931037456, 8.98389684967697, 9.44675268605351,
  13. # 6.60797792486846, 6.2911404389888, 0.617862704675645, 2.05974574899301,
  14. # 1.76556752528995), .Dim = 3:4)
  15. m = as.integer(m)
  16. class(m)
  17. typeof(m)
  18. dput(m)
  19. # > class(m)
  20. # [1] "integer"
  21. # > typeof(m)
  22. # [1] "integer"
  23. # > dput(m)
  24. # c(2L, 3L, 5L, 9L, 2L, 8L, 9L, 6L, 6L, 0L, 2L, 1L)
  25.  
  26.  
  27. #@ ------
  28. set.seed(1)
  29. m = array(runif(12) * 10, c(3, 4))
  30. class(m)
  31. typeof(m)
  32. dput(m)
  33. # > class(m)
  34. # [1] "matrix"
  35. # > typeof(m)
  36. # [1] "double"
  37. # > dput(m)
  38. # structure(c(2.655086631421, 3.7212389963679, 5.72853363351896,
  39. # 9.08207789994776, 2.01681931037456, 8.98389684967697, 9.44675268605351,
  40. # 6.60797792486846, 6.2911404389888, 0.617862704675645, 2.05974574899301,
  41. # 1.76556752528995), .Dim = 3:4)
  42. attributes(m)
  43. # > attributes(m)
  44. # $`dim`
  45. # [1] 3 4
  46. tmp = attributes(m)$`dim`
  47. dput(tmp)
  48. # > dput(tmp)
  49. # 3:4
  50. m = as.integer(m)
  51. attributes(m)$`dim` = tmp
  52. class(m)
  53. typeof(m)
  54. dput(m)
  55. # > class(m)
  56. # [1] "matrix"
  57. # > typeof(m)
  58. # [1] "integer"
  59. # > dput(m)
  60. # structure(c(2L, 3L, 5L, 9L, 2L, 8L, 9L, 6L, 6L, 0L, 2L, 1L), .Dim = 3:4)
  61.  
  62.  
  63. #@ ------
  64. set.seed(1)
  65. m = array(runif(12) * 10, c(3, 4))
  66. class(m)
  67. typeof(m)
  68. dput(m)
  69. # > class(m)
  70. # [1] "matrix"
  71. # > typeof(m)
  72. # [1] "double"
  73. # > dput(m)
  74. # structure(c(2.655086631421, 3.7212389963679, 5.72853363351896,
  75. # 9.08207789994776, 2.01681931037456, 8.98389684967697, 9.44675268605351,
  76. # 6.60797792486846, 6.2911404389888, 0.617862704675645, 2.05974574899301,
  77. # 1.76556752528995), .Dim = 3:4)
  78. as.integer.matrix = function(input) {
  79. if (class(input) != "matrix") {
  80. stop("error1")
  81. } else {
  82. output = as.integer(input)
  83. attributes(output) = attributes(input)
  84. }
  85. output
  86. }
  87. m = as.integer.matrix(m)
  88. class(m)
  89. typeof(m)
  90. dput(m)
  91. # > class(m)
  92. # [1] "matrix"
  93. # > typeof(m)
  94. # [1] "integer"
  95. # > dput(m)
  96. # structure(c(2L, 3L, 5L, 9L, 2L, 8L, 9L, 6L, 6L, 0L, 2L, 1L), .Dim = 3:4)
Add Comment
Please, Sign In to add comment