| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- # Install R
- # # apt install r-base
- # # snap install rstudio
- #
- # Online:
- # https://cran.r-project.org
- # https://posit.co
- # --------------------------------------------------------------------------
- # Slide 4, 5: Installing R
- # --------------------------------------------------------------------------
- # 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)
- q()
- # --------------------------------------------------------------------------
- # Slide 5: Installing R Studio
- # --------------------------------------------------------------------------
- 1 + 2
- 4 * 7
- print("Hello World")
- # --------------------------------------------------------------------------
- # Slide 6: Running R on Posit Cloud
- # --------------------------------------------------------------------------
- 1 + 2
- 4 * 7
- print("Hello World")
- # 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")
- # --------------------------------------------------------------------------
- # Slide 8: Packages in R
- # --------------------------------------------------------------------------
- # 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)
- qplot(data=diamonds, carat, price, colour=clarity, facets = . ~ clarity)
- # --------------------------------------------------------------------------
- # Slide 9: the daily life of a data scientist
- # --------------------------------------------------------------------------
- # 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)
- # Diamonds dataset
- myData <- read.csv("diamonds.csv")
- summary(myData)
- plot(myData$carat, myData$price)
- 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()
|