Commands.R 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. # Install R
  2. # # apt install r-base
  3. # # snap install rstudio
  4. #
  5. # Online:
  6. # https://cran.r-project.org
  7. # https://posit.co
  8. # --------------------------------------------------------------------------
  9. # Slide 4, 5: Installing R
  10. # --------------------------------------------------------------------------
  11. # Some samples
  12. 1 + 2
  13. 4 * 7
  14. (2 + 3) * 9
  15. 2 ^ 3
  16. 11 %% 3
  17. 11 %/% 3
  18. print("Hello World")
  19. print("Hello World", quote=FALSE)
  20. print(81 / 3)
  21. q()
  22. # --------------------------------------------------------------------------
  23. # Slide 5: Installing R Studio
  24. # --------------------------------------------------------------------------
  25. 1 + 2
  26. 4 * 7
  27. print("Hello World")
  28. # --------------------------------------------------------------------------
  29. # Slide 6: Running R on Posit Cloud
  30. # --------------------------------------------------------------------------
  31. 1 + 2
  32. 4 * 7
  33. print("Hello World")
  34. # Some random values:
  35. rnorm(10)
  36. help("rnorm")
  37. # Variables
  38. a <- 3
  39. b <- 5
  40. c <- a * b
  41. print(c)
  42. # Vectors
  43. seq(3, 5, 0.2)
  44. u <- c(1,3,5)
  45. v <- rnorm(3)
  46. v + 5
  47. w <- 7:9
  48. u + w
  49. u - w
  50. u * w
  51. u %*% w
  52. u %o% w
  53. abs(v)
  54. log(w)
  55. log(abs(v))
  56. m <- matrix(1:12, 3, 4)
  57. t(m)
  58. m %*% c(1,2,3,4)
  59. t(sapply(v, function (x) c(x, x * x)))
  60. # Acessing and changing vectors
  61. u[1]
  62. u[2]
  63. u[3]
  64. u
  65. u[2] <- 7
  66. u
  67. u[4] <- 9
  68. u
  69. u[7] <- 11
  70. u
  71. u[1:3]
  72. u[2:5]
  73. u[4:11]
  74. u[is.na(u)] <- 0
  75. u
  76. u[u < 6]
  77. u[u > 6] <- u[u > 6] * 2
  78. # Calculating mean
  79. s <- 0
  80. for (x in w)
  81. {
  82. s <- s + x
  83. }
  84. s / length(w)
  85. mean(w)
  86. # summing up all positive / negative values
  87. s1 <- 0
  88. s2 <- 0
  89. for (x in v)
  90. {
  91. if ( x >= 0 )
  92. {
  93. s1 <- s1 + x
  94. }
  95. else
  96. {
  97. s2 <- s2 + x
  98. }
  99. }
  100. print(c(s1, s2))
  101. sum(v[v >= 0])
  102. sum(v[v < 0])
  103. # Plotting
  104. plot(w, v)
  105. plot(w, v, "l")
  106. help("plot")
  107. # --------------------------------------------------------------------------
  108. # Slide 8: Packages in R
  109. # --------------------------------------------------------------------------
  110. # Install needed package for plotting.
  111. # Needed only once after installing R
  112. install.packages("ggplot2")
  113. # Activate "ggplot2" package.
  114. # Needed only once per session or script.
  115. library(ggplot2)
  116. qplot(data=iris, Sepal.Width, Sepal.Length)
  117. qplot(data=iris, Sepal.Width, Sepal.Length, colour=Species)
  118. qplot(data=iris, Sepal.Width, Sepal.Length, colour=Species, facets = .~ Species)
  119. qplot(data=diamonds, carat, price, colour=clarity, facets = . ~ clarity)
  120. # --------------------------------------------------------------------------
  121. # Slide 9: the daily life of a data scientist
  122. # --------------------------------------------------------------------------
  123. # Datasets
  124. head(iris)
  125. length(iris)
  126. summary(iris)
  127. iris["Sepal.Width"]
  128. iris[c("Sepal.Lenght", "Sepal.Width")]
  129. iris$Sepal.Width
  130. iris$Sepal.Width[1:10]
  131. hist(iris$Sepal.Width)
  132. hist(iris$Sepal.Length)
  133. boxplot(iris$Sepal.Length ~ iris$Species)
  134. plot(iris$Sepal.Width, iris$Sepal.Length)
  135. # Diamonds dataset
  136. myData <- read.csv("diamonds.csv")
  137. summary(myData)
  138. plot(myData$carat, myData$price)
  139. ggplot(data=myData, aes(x=carat, y=price)) + geom_point()
  140. ggplot(data=myData, aes(x=carat, y=price, color=clarity)) + geom_point()
  141. ggplot(data=myData[myData$carat < 2.5,], aes(x=carat, y=price, color=clarity)) + geom_point(alpha=0.1) + geom_smooth()