class: center, middle, inverse, title-slide # intRo: Data visualisation with R ## 02 — R Basics ### Stefano Coretta --- # R as a calculator Write the following in the `Console`, then press `ENTER`. ```r 1 + 2 ``` ``` ## [1] 3 ``` -- You did it! You run your first line of code! --- # R as a calculator Try some more operations... ```r 67 - 13 ``` ``` ## [1] 54 ``` ```r 2 * 4 ``` ``` ## [1] 8 ``` ```r 268 / 43 ``` ``` ## [1] 6.232558 ``` --- # R as a calculator You can chain operations... ```r 6 + 4 - 1 + 2 ``` ``` ## [1] 11 ``` ```r 4 * 2 + 3 * 2 ``` ``` ## [1] 14 ``` --- # R as a calculator But watch out for your brackets! ```r 4 * 2 + 3 * 2 ``` ``` ## [1] 14 ``` ```r (4 * 2) + (3 * 2) ``` ``` ## [1] 14 ``` ```r 4 * (2 + 3) * 2 ``` ``` ## [1] 40 ``` --- # Forget-me-not: variables Store a variable in the computer memory for later use. ```r my_num <- 156 ``` -- You can just call it back when you need it! ```r my_num ``` ``` ## [1] 156 ``` --- # Oh my vars! ```r income <- 1200 expenses <- 500 income - expenses ``` ``` ## [1] 700 ``` -- You can go all the way with variables! ```r savings <- income - expenses ``` And check the value... ```r savings ``` ``` ## [1] 700 ``` --- # Variables are... variable ```r yo <- 5 yo + 3 ``` ``` ## [1] 8 ``` ```r yo <- 3 yo + 3 ``` ``` ## [1] 6 ``` --- # Variables can hold more than one item ```r one_i <- 6 two_i <- c(6, 8) five_i <- c(6, 8, 42) ``` -- Note the following are the same: ```r one_i <- 6 one_ii <- c(6) ``` --- # R can't function without... functions **Functions** are operations performed on one or more *arguments*. ```r sum(3, 5) ``` ``` ## [1] 8 ``` -- And arguments can be vectors! ```r my_nums <- c(3, 5, 7) sum(my_nums) ``` ``` ## [1] 15 ``` ```r mean(my_nums) ``` ``` ## [1] 5 ``` --- # Not just numbers Variables can store **strings**. ```r name <- "Stefano" surname <- "Coretta" name ``` ``` ## [1] "Stefano" ``` --- # Not just numbers And strings can be used as arguments in functions. ```r cat("My name is", name, surname) ``` ``` ## My name is Stefano Coretta ``` -- And you can of course reuse the same variable name to override the variable value. ```r name <- "Raj" cat("My name is", name) ``` ``` ## My name is Raj ``` --- # The null and the infinite ```r forgot <- NA nothing <- NULL everything <- Inf ``` --- # Summary - R as a calculator - Arithmetic operations. - Variable: - Numeric. - String. - Function: - Function name (`sum()`, `mean()`, `cat()`). - Arguments. --- class: middle center inverse .f1[TUTORIAL]