Introduction to R

Hello World!

In [102]:
print("Hello World!")

print("Hello World!", quote = FALSE)

print(paste("Hello","World","!"))
[1] "Hello World!"
[1] Hello World!
[1] "Hello World !"

Using R interactively

$ R - start the R program

> q() - quit the R program

> help("topic") - help facility similar to the man facility of unix

> ?"topic" - help facility similar to the man facility of unix

> source("file.R") - execute commands stored in file.R


Installing packages

> install.packages("package_name")


Loading packages

> require(package_name)

> library(package_name)

With pacman

> library(pacman)

> pacman::p_load(package_name)

You can use pacman to install packages


Unload packages

> detach(package_name)

With pacman

> p_unload(package_name)

> p_unload(all)

Native package can't be loaded or unloaded with pacman

Pacman is a package, you should install it first


Playing with iris dataset

In [103]:
iris
A data.frame: 150 × 5
Sepal.LengthSepal.WidthPetal.LengthPetal.WidthSpecies
<dbl><dbl><dbl><dbl><fct>
5.13.51.40.2setosa
4.93.01.40.2setosa
4.73.21.30.2setosa
4.63.11.50.2setosa
5.03.61.40.2setosa
5.43.91.70.4setosa
4.63.41.40.3setosa
5.03.41.50.2setosa
4.42.91.40.2setosa
4.93.11.50.1setosa
5.43.71.50.2setosa
4.83.41.60.2setosa
4.83.01.40.1setosa
4.33.01.10.1setosa
5.84.01.20.2setosa
5.74.41.50.4setosa
5.43.91.30.4setosa
5.13.51.40.3setosa
5.73.81.70.3setosa
5.13.81.50.3setosa
5.43.41.70.2setosa
5.13.71.50.4setosa
4.63.61.00.2setosa
5.13.31.70.5setosa
4.83.41.90.2setosa
5.03.01.60.2setosa
5.03.41.60.4setosa
5.23.51.50.2setosa
5.23.41.40.2setosa
4.73.21.60.2setosa
⋮⋮⋮⋮⋮
6.93.25.72.3virginica
5.62.84.92.0virginica
7.72.86.72.0virginica
6.32.74.91.8virginica
6.73.35.72.1virginica
7.23.26.01.8virginica
6.22.84.81.8virginica
6.13.04.91.8virginica
6.42.85.62.1virginica
7.23.05.81.6virginica
7.42.86.11.9virginica
7.93.86.42.0virginica
6.42.85.62.2virginica
6.32.85.11.5virginica
6.12.65.61.4virginica
7.73.06.12.3virginica
6.33.45.62.4virginica
6.43.15.51.8virginica
6.03.04.81.8virginica
6.93.15.42.1virginica
6.73.15.62.4virginica
6.93.15.12.3virginica
5.82.75.11.9virginica
6.83.25.92.3virginica
6.73.35.72.5virginica
6.73.05.22.3virginica
6.32.55.01.9virginica
6.53.05.22.0virginica
6.23.45.42.3virginica
5.93.05.11.8virginica

Ploting iris dataset

In [104]:
plot(iris)

Select Sepal.Length

In [105]:
x <- iris[,1]
x
  1. 5.1
  2. 4.9
  3. 4.7
  4. 4.6
  5. 5
  6. 5.4
  7. 4.6
  8. 5
  9. 4.4
  10. 4.9
  11. 5.4
  12. 4.8
  13. 4.8
  14. 4.3
  15. 5.8
  16. 5.7
  17. 5.4
  18. 5.1
  19. 5.7
  20. 5.1
  21. 5.4
  22. 5.1
  23. 4.6
  24. 5.1
  25. 4.8
  26. 5
  27. 5
  28. 5.2
  29. 5.2
  30. 4.7
  31. 4.8
  32. 5.4
  33. 5.2
  34. 5.5
  35. 4.9
  36. 5
  37. 5.5
  38. 4.9
  39. 4.4
  40. 5.1
  41. 5
  42. 4.5
  43. 4.4
  44. 5
  45. 5.1
  46. 4.8
  47. 5.1
  48. 4.6
  49. 5.3
  50. 5
  51. 7
  52. 6.4
  53. 6.9
  54. 5.5
  55. 6.5
  56. 5.7
  57. 6.3
  58. 4.9
  59. 6.6
  60. 5.2
  61. 5
  62. 5.9
  63. 6
  64. 6.1
  65. 5.6
  66. 6.7
  67. 5.6
  68. 5.8
  69. 6.2
  70. 5.6
  71. 5.9
  72. 6.1
  73. 6.3
  74. 6.1
  75. 6.4
  76. 6.6
  77. 6.8
  78. 6.7
  79. 6
  80. 5.7
  81. 5.5
  82. 5.5
  83. 5.8
  84. 6
  85. 5.4
  86. 6
  87. 6.7
  88. 6.3
  89. 5.6
  90. 5.5
  91. 5.5
  92. 6.1
  93. 5.8
  94. 5
  95. 5.6
  96. 5.7
  97. 5.7
  98. 6.2
  99. 5.1
  100. 5.7
  101. 6.3
  102. 5.8
  103. 7.1
  104. 6.3
  105. 6.5
  106. 7.6
  107. 4.9
  108. 7.3
  109. 6.7
  110. 7.2
  111. 6.5
  112. 6.4
  113. 6.8
  114. 5.7
  115. 5.8
  116. 6.4
  117. 6.5
  118. 7.7
  119. 7.7
  120. 6
  121. 6.9
  122. 5.6
  123. 7.7
  124. 6.3
  125. 6.7
  126. 7.2
  127. 6.2
  128. 6.1
  129. 6.4
  130. 7.2
  131. 7.4
  132. 7.9
  133. 6.4
  134. 6.3
  135. 6.1
  136. 7.7
  137. 6.3
  138. 6.4
  139. 6
  140. 6.9
  141. 6.7
  142. 6.9
  143. 5.8
  144. 6.8
  145. 6.7
  146. 6.7
  147. 6.3
  148. 6.5
  149. 6.2
  150. 5.9
In [106]:
summary(x)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  4.300   5.100   5.800   5.843   6.400   7.900 

Discretize dataset by frequency

In [107]:
require(arules)
discretize(x)
  1. [4.3,5.4)
  2. [4.3,5.4)
  3. [4.3,5.4)
  4. [4.3,5.4)
  5. [4.3,5.4)
  6. [5.4,6.3)
  7. [4.3,5.4)
  8. [4.3,5.4)
  9. [4.3,5.4)
  10. [4.3,5.4)
  11. [5.4,6.3)
  12. [4.3,5.4)
  13. [4.3,5.4)
  14. [4.3,5.4)
  15. [5.4,6.3)
  16. [5.4,6.3)
  17. [5.4,6.3)
  18. [4.3,5.4)
  19. [5.4,6.3)
  20. [4.3,5.4)
  21. [5.4,6.3)
  22. [4.3,5.4)
  23. [4.3,5.4)
  24. [4.3,5.4)
  25. [4.3,5.4)
  26. [4.3,5.4)
  27. [4.3,5.4)
  28. [4.3,5.4)
  29. [4.3,5.4)
  30. [4.3,5.4)
  31. [4.3,5.4)
  32. [5.4,6.3)
  33. [4.3,5.4)
  34. [5.4,6.3)
  35. [4.3,5.4)
  36. [4.3,5.4)
  37. [5.4,6.3)
  38. [4.3,5.4)
  39. [4.3,5.4)
  40. [4.3,5.4)
  41. [4.3,5.4)
  42. [4.3,5.4)
  43. [4.3,5.4)
  44. [4.3,5.4)
  45. [4.3,5.4)
  46. [4.3,5.4)
  47. [4.3,5.4)
  48. [4.3,5.4)
  49. [4.3,5.4)
  50. [4.3,5.4)
  51. [6.3,7.9]
  52. [6.3,7.9]
  53. [6.3,7.9]
  54. [5.4,6.3)
  55. [6.3,7.9]
  56. [5.4,6.3)
  57. [6.3,7.9]
  58. [4.3,5.4)
  59. [6.3,7.9]
  60. [4.3,5.4)
  61. [4.3,5.4)
  62. [5.4,6.3)
  63. [5.4,6.3)
  64. [5.4,6.3)
  65. [5.4,6.3)
  66. [6.3,7.9]
  67. [5.4,6.3)
  68. [5.4,6.3)
  69. [5.4,6.3)
  70. [5.4,6.3)
  71. [5.4,6.3)
  72. [5.4,6.3)
  73. [6.3,7.9]
  74. [5.4,6.3)
  75. [6.3,7.9]
  76. [6.3,7.9]
  77. [6.3,7.9]
  78. [6.3,7.9]
  79. [5.4,6.3)
  80. [5.4,6.3)
  81. [5.4,6.3)
  82. [5.4,6.3)
  83. [5.4,6.3)
  84. [5.4,6.3)
  85. [5.4,6.3)
  86. [5.4,6.3)
  87. [6.3,7.9]
  88. [6.3,7.9]
  89. [5.4,6.3)
  90. [5.4,6.3)
  91. [5.4,6.3)
  92. [5.4,6.3)
  93. [5.4,6.3)
  94. [4.3,5.4)
  95. [5.4,6.3)
  96. [5.4,6.3)
  97. [5.4,6.3)
  98. [5.4,6.3)
  99. [4.3,5.4)
  100. [5.4,6.3)
  101. [6.3,7.9]
  102. [5.4,6.3)
  103. [6.3,7.9]
  104. [6.3,7.9]
  105. [6.3,7.9]
  106. [6.3,7.9]
  107. [4.3,5.4)
  108. [6.3,7.9]
  109. [6.3,7.9]
  110. [6.3,7.9]
  111. [6.3,7.9]
  112. [6.3,7.9]
  113. [6.3,7.9]
  114. [5.4,6.3)
  115. [5.4,6.3)
  116. [6.3,7.9]
  117. [6.3,7.9]
  118. [6.3,7.9]
  119. [6.3,7.9]
  120. [5.4,6.3)
  121. [6.3,7.9]
  122. [5.4,6.3)
  123. [6.3,7.9]
  124. [6.3,7.9]
  125. [6.3,7.9]
  126. [6.3,7.9]
  127. [5.4,6.3)
  128. [5.4,6.3)
  129. [6.3,7.9]
  130. [6.3,7.9]
  131. [6.3,7.9]
  132. [6.3,7.9]
  133. [6.3,7.9]
  134. [6.3,7.9]
  135. [5.4,6.3)
  136. [6.3,7.9]
  137. [6.3,7.9]
  138. [6.3,7.9]
  139. [5.4,6.3)
  140. [6.3,7.9]
  141. [6.3,7.9]
  142. [6.3,7.9]
  143. [5.4,6.3)
  144. [6.3,7.9]
  145. [6.3,7.9]
  146. [6.3,7.9]
  147. [6.3,7.9]
  148. [6.3,7.9]
  149. [5.4,6.3)
  150. [5.4,6.3)
Levels:
  1. '[4.3,5.4)'
  2. '[5.4,6.3)'
  3. '[6.3,7.9]'

To use discretize you neet to import the arules package

> install.packages(arules)

> require(arules)

In [108]:
table(discretize(x))
[4.3,5.4) [5.4,6.3) [6.3,7.9] 
       46        53        51 
In [109]:
discretize(x, breaks = 10)
  1. [5,5.27)
  2. [4.8,5)
  3. [4.3,4.8)
  4. [4.3,4.8)
  5. [5,5.27)
  6. [5.27,5.6)
  7. [4.3,4.8)
  8. [5,5.27)
  9. [4.3,4.8)
  10. [4.8,5)
  11. [5.27,5.6)
  12. [4.8,5)
  13. [4.8,5)
  14. [4.3,4.8)
  15. [5.8,6.1)
  16. [5.6,5.8)
  17. [5.27,5.6)
  18. [5,5.27)
  19. [5.6,5.8)
  20. [5,5.27)
  21. [5.27,5.6)
  22. [5,5.27)
  23. [4.3,4.8)
  24. [5,5.27)
  25. [4.8,5)
  26. [5,5.27)
  27. [5,5.27)
  28. [5,5.27)
  29. [5,5.27)
  30. [4.3,4.8)
  31. [4.8,5)
  32. [5.27,5.6)
  33. [5,5.27)
  34. [5.27,5.6)
  35. [4.8,5)
  36. [5,5.27)
  37. [5.27,5.6)
  38. [4.8,5)
  39. [4.3,4.8)
  40. [5,5.27)
  41. [5,5.27)
  42. [4.3,4.8)
  43. [4.3,4.8)
  44. [5,5.27)
  45. [5,5.27)
  46. [4.8,5)
  47. [5,5.27)
  48. [4.3,4.8)
  49. [5.27,5.6)
  50. [5,5.27)
  51. [6.9,7.9]
  52. [6.3,6.52)
  53. [6.9,7.9]
  54. [5.27,5.6)
  55. [6.3,6.52)
  56. [5.6,5.8)
  57. [6.3,6.52)
  58. [4.8,5)
  59. [6.52,6.9)
  60. [5,5.27)
  61. [5,5.27)
  62. [5.8,6.1)
  63. [5.8,6.1)
  64. [6.1,6.3)
  65. [5.6,5.8)
  66. [6.52,6.9)
  67. [5.6,5.8)
  68. [5.8,6.1)
  69. [6.1,6.3)
  70. [5.6,5.8)
  71. [5.8,6.1)
  72. [6.1,6.3)
  73. [6.3,6.52)
  74. [6.1,6.3)
  75. [6.3,6.52)
  76. [6.52,6.9)
  77. [6.52,6.9)
  78. [6.52,6.9)
  79. [5.8,6.1)
  80. [5.6,5.8)
  81. [5.27,5.6)
  82. [5.27,5.6)
  83. [5.8,6.1)
  84. [5.8,6.1)
  85. [5.27,5.6)
  86. [5.8,6.1)
  87. [6.52,6.9)
  88. [6.3,6.52)
  89. [5.6,5.8)
  90. [5.27,5.6)
  91. [5.27,5.6)
  92. [6.1,6.3)
  93. [5.8,6.1)
  94. [5,5.27)
  95. [5.6,5.8)
  96. [5.6,5.8)
  97. [5.6,5.8)
  98. [6.1,6.3)
  99. [5,5.27)
  100. [5.6,5.8)
  101. [6.3,6.52)
  102. [5.8,6.1)
  103. [6.9,7.9]
  104. [6.3,6.52)
  105. [6.3,6.52)
  106. [6.9,7.9]
  107. [4.8,5)
  108. [6.9,7.9]
  109. [6.52,6.9)
  110. [6.9,7.9]
  111. [6.3,6.52)
  112. [6.3,6.52)
  113. [6.52,6.9)
  114. [5.6,5.8)
  115. [5.8,6.1)
  116. [6.3,6.52)
  117. [6.3,6.52)
  118. [6.9,7.9]
  119. [6.9,7.9]
  120. [5.8,6.1)
  121. [6.9,7.9]
  122. [5.6,5.8)
  123. [6.9,7.9]
  124. [6.3,6.52)
  125. [6.52,6.9)
  126. [6.9,7.9]
  127. [6.1,6.3)
  128. [6.1,6.3)
  129. [6.3,6.52)
  130. [6.9,7.9]
  131. [6.9,7.9]
  132. [6.9,7.9]
  133. [6.3,6.52)
  134. [6.3,6.52)
  135. [6.1,6.3)
  136. [6.9,7.9]
  137. [6.3,6.52)
  138. [6.3,6.52)
  139. [5.8,6.1)
  140. [6.9,7.9]
  141. [6.52,6.9)
  142. [6.9,7.9]
  143. [5.8,6.1)
  144. [6.52,6.9)
  145. [6.52,6.9)
  146. [6.52,6.9)
  147. [6.3,6.52)
  148. [6.3,6.52)
  149. [6.1,6.3)
  150. [5.8,6.1)
Levels:
  1. '[4.3,4.8)'
  2. '[4.8,5)'
  3. '[5,5.27)'
  4. '[5.27,5.6)'
  5. '[5.6,5.8)'
  6. '[5.8,6.1)'
  7. '[6.1,6.3)'
  8. '[6.3,6.52)'
  9. '[6.52,6.9)'
  10. '[6.9,7.9]'
In [110]:
table(discretize(x, breaks = 10))
 [4.3,4.8)    [4.8,5)   [5,5.27) [5.27,5.6)  [5.6,5.8)  [5.8,6.1)  [6.1,6.3) 
        11         11         23         14         14         16         10 
[6.3,6.52) [6.52,6.9)  [6.9,7.9] 
        21         13         17 

Discretize dataset by intervals

In [111]:
discretize(x, method = "interval", breaks = 3)
  1. [4.3,5.5)
  2. [4.3,5.5)
  3. [4.3,5.5)
  4. [4.3,5.5)
  5. [4.3,5.5)
  6. [4.3,5.5)
  7. [4.3,5.5)
  8. [4.3,5.5)
  9. [4.3,5.5)
  10. [4.3,5.5)
  11. [4.3,5.5)
  12. [4.3,5.5)
  13. [4.3,5.5)
  14. [4.3,5.5)
  15. [5.5,6.7)
  16. [5.5,6.7)
  17. [4.3,5.5)
  18. [4.3,5.5)
  19. [5.5,6.7)
  20. [4.3,5.5)
  21. [4.3,5.5)
  22. [4.3,5.5)
  23. [4.3,5.5)
  24. [4.3,5.5)
  25. [4.3,5.5)
  26. [4.3,5.5)
  27. [4.3,5.5)
  28. [4.3,5.5)
  29. [4.3,5.5)
  30. [4.3,5.5)
  31. [4.3,5.5)
  32. [4.3,5.5)
  33. [4.3,5.5)
  34. [5.5,6.7)
  35. [4.3,5.5)
  36. [4.3,5.5)
  37. [5.5,6.7)
  38. [4.3,5.5)
  39. [4.3,5.5)
  40. [4.3,5.5)
  41. [4.3,5.5)
  42. [4.3,5.5)
  43. [4.3,5.5)
  44. [4.3,5.5)
  45. [4.3,5.5)
  46. [4.3,5.5)
  47. [4.3,5.5)
  48. [4.3,5.5)
  49. [4.3,5.5)
  50. [4.3,5.5)
  51. [6.7,7.9]
  52. [5.5,6.7)
  53. [6.7,7.9]
  54. [5.5,6.7)
  55. [5.5,6.7)
  56. [5.5,6.7)
  57. [5.5,6.7)
  58. [4.3,5.5)
  59. [5.5,6.7)
  60. [4.3,5.5)
  61. [4.3,5.5)
  62. [5.5,6.7)
  63. [5.5,6.7)
  64. [5.5,6.7)
  65. [5.5,6.7)
  66. [6.7,7.9]
  67. [5.5,6.7)
  68. [5.5,6.7)
  69. [5.5,6.7)
  70. [5.5,6.7)
  71. [5.5,6.7)
  72. [5.5,6.7)
  73. [5.5,6.7)
  74. [5.5,6.7)
  75. [5.5,6.7)
  76. [5.5,6.7)
  77. [6.7,7.9]
  78. [6.7,7.9]
  79. [5.5,6.7)
  80. [5.5,6.7)
  81. [5.5,6.7)
  82. [5.5,6.7)
  83. [5.5,6.7)
  84. [5.5,6.7)
  85. [4.3,5.5)
  86. [5.5,6.7)
  87. [6.7,7.9]
  88. [5.5,6.7)
  89. [5.5,6.7)
  90. [5.5,6.7)
  91. [5.5,6.7)
  92. [5.5,6.7)
  93. [5.5,6.7)
  94. [4.3,5.5)
  95. [5.5,6.7)
  96. [5.5,6.7)
  97. [5.5,6.7)
  98. [5.5,6.7)
  99. [4.3,5.5)
  100. [5.5,6.7)
  101. [5.5,6.7)
  102. [5.5,6.7)
  103. [6.7,7.9]
  104. [5.5,6.7)
  105. [5.5,6.7)
  106. [6.7,7.9]
  107. [4.3,5.5)
  108. [6.7,7.9]
  109. [6.7,7.9]
  110. [6.7,7.9]
  111. [5.5,6.7)
  112. [5.5,6.7)
  113. [6.7,7.9]
  114. [5.5,6.7)
  115. [5.5,6.7)
  116. [5.5,6.7)
  117. [5.5,6.7)
  118. [6.7,7.9]
  119. [6.7,7.9]
  120. [5.5,6.7)
  121. [6.7,7.9]
  122. [5.5,6.7)
  123. [6.7,7.9]
  124. [5.5,6.7)
  125. [6.7,7.9]
  126. [6.7,7.9]
  127. [5.5,6.7)
  128. [5.5,6.7)
  129. [5.5,6.7)
  130. [6.7,7.9]
  131. [6.7,7.9]
  132. [6.7,7.9]
  133. [5.5,6.7)
  134. [5.5,6.7)
  135. [5.5,6.7)
  136. [6.7,7.9]
  137. [5.5,6.7)
  138. [5.5,6.7)
  139. [5.5,6.7)
  140. [6.7,7.9]
  141. [6.7,7.9]
  142. [6.7,7.9]
  143. [5.5,6.7)
  144. [6.7,7.9]
  145. [6.7,7.9]
  146. [6.7,7.9]
  147. [5.5,6.7)
  148. [5.5,6.7)
  149. [5.5,6.7)
  150. [5.5,6.7)
Levels:
  1. '[4.3,5.5)'
  2. '[5.5,6.7)'
  3. '[6.7,7.9]'
In [112]:
table(discretize(x, method = "interval", breaks = 3))
[4.3,5.5) [5.5,6.7) [6.7,7.9] 
       52        70        28 

Discretize each numeric column

In [113]:
irisDisc <- discretizeDF(iris)
head(irisDisc)
A data.frame: 6 × 5
Sepal.LengthSepal.WidthPetal.LengthPetal.WidthSpecies
<fct><fct><fct><fct><fct>
1[4.3,5.4)[3.2,4.4][1,2.63)[0.1,0.867)setosa
2[4.3,5.4)[2.9,3.2)[1,2.63)[0.1,0.867)setosa
3[4.3,5.4)[3.2,4.4][1,2.63)[0.1,0.867)setosa
4[4.3,5.4)[2.9,3.2)[1,2.63)[0.1,0.867)setosa
5[4.3,5.4)[3.2,4.4][1,2.63)[0.1,0.867)setosa
6[5.4,6.3)[3.2,4.4][1,2.63)[0.1,0.867)setosa

Discretize each numeric column with labels

In [114]:
irisDisc <- discretizeDF(iris, default = list(method = "interval", breaks = 2, labels = c("small", "large")))
head(irisDisc)
A data.frame: 6 × 5
Sepal.LengthSepal.WidthPetal.LengthPetal.WidthSpecies
<fct><fct><fct><fct><fct>
1smalllargesmallsmallsetosa
2smallsmallsmallsmallsetosa
3smalllargesmallsmallsetosa
4smallsmallsmallsmallsetosa
5smalllargesmallsmallsetosa
6smalllargesmallsmallsetosa
In [115]:
irisDiscB <- discretizeDF(iris, methods = list(
Petal.Length = list(method = "frequency", breaks = 3, labels = c("short", "medium", "long")),
Petal.Width = list(method = "frequency", breaks = 2, labels = c("narrow", "wide"))
))
head(irisDiscB)
A data.frame: 6 × 5
Sepal.LengthSepal.WidthPetal.LengthPetal.WidthSpecies
<fct><fct><fct><fct><fct>
1[4.3,5.4)[3.2,4.4]shortnarrowsetosa
2[4.3,5.4)[2.9,3.2)shortnarrowsetosa
3[4.3,5.4)[3.2,4.4]shortnarrowsetosa
4[4.3,5.4)[2.9,3.2)shortnarrowsetosa
5[4.3,5.4)[3.2,4.4]shortnarrowsetosa
6[5.4,6.3)[3.2,4.4]shortnarrowsetosa

Standardization

In [128]:
set.seed(1234) # for reproducibility
x <- rnorm(10) # standard normal
x
  1. -1.20706574938542
  2. 0.27742924211066
  3. 1.08444117668306
  4. -2.34569770262935
  5. 0.42912468881105
  6. 0.506055892157574
  7. -0.574739960134649
  8. -0.546631855784187
  9. -0.564451999093283
  10. -0.890037829044104

To use standardize you neet to import the robustHD package

> install.packages(arules)

> require(arules)

In [127]:
require(robustHD)
standardize(x) # mean and sd
  1. -0.827393701468607
  2. 0.66338111723447
  3. 1.47380693702179
  4. -1.97084238495393
  5. 0.815718277917611
  6. 0.892974921677615
  7. -0.192392998043136
  8. -0.164165988583901
  9. -0.182061516061638
  10. -0.509024664740275
In [129]:
summary(x)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
-2.3457 -0.8112 -0.5555 -0.3832  0.3912  1.0844 
In [130]:
summary(standardize(x))
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
-1.9708 -0.4299 -0.1731  0.0000  0.7776  1.4738 
In [132]:
sd(x)
0.995787541399137
In [134]:
sd(standardize(x))
1

Basic graphics

Categorical variable

In [147]:
plot(iris$Species)

Quantitative variable

In [148]:
plot(iris$Petal.Length)

Cat x quant

In [150]:
plot(iris$Species, iris$Petal.Width)

Quant pair

In [152]:
plot(iris$Petal.Length, iris$Petal.Width)

Entire data frame

In [153]:
plot(iris)

Plot options

In [156]:
plot(iris$Petal.Length, iris$Petal.Width,
col = "#cc0000",
pch = 19,
main = "Iris: Petal Length vs. Petal Width",
xlab = "Petal Length",
ylab = "Petal Width"
)

Plot formulas

In [157]:
plot(cos, 0, 2*pi)
In [158]:
plot(exp, 1, 5)
In [159]:
plot(dnorm, -3, +3)

Bar charsts

In [173]:
barplot(mtcars$gear)

We need a table with frequencies for each category.

In [181]:
barplot(table(mtcars$gear))

Histograms

In [194]:
hist(iris$Sepal.Length)
In [207]:
# Put graphs in 3 rows and 1 column
par(mfrow = c(3,1))

# Histograms for setosa
hist(iris$Petal.Width [iris$Species == "setosa"],
xlim = c(0,3),
breaks = 9,
main = "Petal Width for Setosa",
xlab = "",
col = "red"
)

# Histograms for versicolor
hist(iris$Petal.Width [iris$Species == "versicolor"],
xlim = c(0,3),
breaks = 9,
main = "Petal Width for Versicolor",
xlab = "",
col = "green"
)

# Histograms for virginica
hist(iris$Petal.Width [iris$Species == "virginica"],
xlim = c(0,3),
breaks = 9,
main = "Petal Width for Virginica",
xlab = "",
col = "blue"
)