Commands.R 2.1 KB

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