# Install R # # apt install r-base # # snap install rstudio # # Online: # https://cran.r-project.org # https://posit.co # Some samples 1 + 2 4 * 7 (2 + 3) * 9 2 ^ 3 11 %% 3 11 %/% 3 print("Hello World") print("Hello World", quote=FALSE) print(81 / 3) # Some random values: rnorm(10) help("rnorm") # Variables a <- 3 b <- 5 c <- a * b print(c) # Vectors seq(3, 5, 0.2) u <- c(1,3,5) v <- rnorm(3) v + 5 w <- 7:9 u + w u - w u * w u %*% w u %o% w abs(v) log(w) log(abs(v)) m <- matrix(1:12, 3, 4) t(m) m %*% c(1,2,3,4) t(sapply(v, function (x) c(x, x * x))) # Acessing and changing vectors u[1] u[2] u[3] u u[2] <- 7 u u[4] <- 9 u u[7] <- 11 u u[1:3] u[2:5] u[4:11] u[is.na(u)] <- 0 u u[u < 6] u[u > 6] <- u[u > 6] * 2 # Calculating mean s <- 0 for (x in w) { s <- s + x } s / length(w) mean(w) # summing up all positive / negative values s1 <- 0 s2 <- 0 for (x in v) { if ( x >= 0 ) { s1 <- s1 + x } else { s2 <- s2 + x } } print(c(s1, s2)) sum(v[v >= 0]) sum(v[v < 0]) # Plotting plot(w, v) plot(w, v, "l") help("plot") # Datasets head(iris) length(iris) summary(iris) iris["Sepal.Width"] iris[c("Sepal.Lenght", "Sepal.Width")] iris$Sepal.Width iris$Sepal.Width[1:10] hist(iris$Sepal.Width) hist(iris$Sepal.Length) boxplot(iris$Sepal.Length ~ iris$Species) plot(iris$Sepal.Width, iris$Sepal.Length) # Install needed package for plotting. # Needed only once after installing R install.packages("ggplot2") # Activate "ggplot2" package. # Needed only once per session or script. library(ggplot2) qplot(data=iris, Sepal.Width, Sepal.Length) qplot(data=iris, Sepal.Width, Sepal.Length, colour=Species) qplot(data=iris, Sepal.Width, Sepal.Length, colour=Species, facets = .~ Species) # Diamonds dataset myData <- read.csv("diamonds.csv") summary(myData) plot(myData$carat, myData$price) qplot(data=diamonds, carat, price, colour=clarity, facets = . ~ clarity) ggplot(data=myData, aes(x=carat, y=price)) + geom_point() ggplot(data=myData, aes(x=carat, y=price, color=clarity)) + geom_point() ggplot(data=myData[myData$carat < 2.5,], aes(x=carat, y=price, color=clarity)) + geom_point(alpha=0.1) + geom_smooth()