Hands On with R

Created by Ramses Alexander Coraspe Valdez

Created on July 1, 2020

An object can be created with the operator <- or -> depending on the direction in which the object is assigned:

In [3]:
n <- 15
n
5 -> n
n
x <- 1
X <- 10
x
X
15
5
1
10

Every object has two intrinsic attributes: type and length

In [5]:
x <- 1
mode(x)
length(x)
A <- "Ramses"; compar <- TRUE; z <- 1i
mode(A); mode(compar); mode(z)
'numeric'
1
'character'
'logical'
'complex'

Vectors

In [7]:
# vectores, arrays
vector.1 <- c(1,10,100,1000) 
vector.1[4] #1-based index
vector.1 + 1 
vector.1 * 2
vector.2 <- c(8, 15)
vector.2
1000
  1. 2
  2. 11
  3. 101
  4. 1001
  1. 2
  2. 20
  3. 200
  4. 2000
  1. 8
  2. 15

DataFrames

In [10]:
x <- 1:4; n <- 10; M <- c(10, 35); y <- 2:4
data.frame(x, n)

data.frame(x, M)
A data.frame: 4 × 2
xn
<int><dbl>
110
210
310
410
A data.frame: 4 × 2
xM
<int><dbl>
110
235
310
435

Lists

In [13]:
Lista1 <- list(x, y); Lista2 <- list(A=x, B=y)
Lista1
Lista2
names(Lista1)
names(Lista2)
    1. 1
    2. 2
    3. 3
    4. 4
    1. 2
    2. 3
    3. 4
$A
  1. 1
  2. 2
  3. 3
  4. 4
$B
  1. 2
  2. 3
  3. 4
NULL
  1. 'A'
  2. 'B'

Arrays

In [14]:
# matrices
1:100
100:1
seq(1,100, 4)
?matrix
mtx.1 <- matrix(data=1:100, nrow=10, ncol=11)
mtx.1
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 62
  63. 63
  64. 64
  65. 65
  66. 66
  67. 67
  68. 68
  69. 69
  70. 70
  71. 71
  72. 72
  73. 73
  74. 74
  75. 75
  76. 76
  77. 77
  78. 78
  79. 79
  80. 80
  81. 81
  82. 82
  83. 83
  84. 84
  85. 85
  86. 86
  87. 87
  88. 88
  89. 89
  90. 90
  91. 91
  92. 92
  93. 93
  94. 94
  95. 95
  96. 96
  97. 97
  98. 98
  99. 99
  100. 100
  1. 100
  2. 99
  3. 98
  4. 97
  5. 96
  6. 95
  7. 94
  8. 93
  9. 92
  10. 91
  11. 90
  12. 89
  13. 88
  14. 87
  15. 86
  16. 85
  17. 84
  18. 83
  19. 82
  20. 81
  21. 80
  22. 79
  23. 78
  24. 77
  25. 76
  26. 75
  27. 74
  28. 73
  29. 72
  30. 71
  31. 70
  32. 69
  33. 68
  34. 67
  35. 66
  36. 65
  37. 64
  38. 63
  39. 62
  40. 61
  41. 60
  42. 59
  43. 58
  44. 57
  45. 56
  46. 55
  47. 54
  48. 53
  49. 52
  50. 51
  51. 50
  52. 49
  53. 48
  54. 47
  55. 46
  56. 45
  57. 44
  58. 43
  59. 42
  60. 41
  61. 40
  62. 39
  63. 38
  64. 37
  65. 36
  66. 35
  67. 34
  68. 33
  69. 32
  70. 31
  71. 30
  72. 29
  73. 28
  74. 27
  75. 26
  76. 25
  77. 24
  78. 23
  79. 22
  80. 21
  81. 20
  82. 19
  83. 18
  84. 17
  85. 16
  86. 15
  87. 14
  88. 13
  89. 12
  90. 11
  91. 10
  92. 9
  93. 8
  94. 7
  95. 6
  96. 5
  97. 4
  98. 3
  99. 2
  100. 1
  1. 1
  2. 5
  3. 9
  4. 13
  5. 17
  6. 21
  7. 25
  8. 29
  9. 33
  10. 37
  11. 41
  12. 45
  13. 49
  14. 53
  15. 57
  16. 61
  17. 65
  18. 69
  19. 73
  20. 77
  21. 81
  22. 85
  23. 89
  24. 93
  25. 97
Warning message in matrix(data = 1:100, nrow = 10, ncol = 11):
“data length [100] is not a sub-multiple or multiple of the number of columns [11]”
A matrix: 10 × 11 of type int
11121314151617181 91 1
21222324252627282 92 2
31323334353637383 93 3
41424344454647484 94 4
51525354555657585 95 5
61626364656667686 96 6
71727374757677787 97 7
81828384858687888 98 8
91929394959697989 99 9
10203040506070809010010