class: center, middle, inverse, title-slide .title[ # Variables, Data types & Operators ] .subtitle[ ## Elements of the R language ] .author[ ### Marcin Kierczak & Nima Rafati ] --- exclude: true count: false <link href="https://fonts.googleapis.com/css?family=Roboto|Source+Sans+Pro:300,400,600|Ubuntu+Mono&subset=latin-ext" rel="stylesheet"> <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.3.1/css/all.css" integrity="sha384-mzrmE5qonljUremFsqc01SB46JvROS7bZs3IO2EmfFsd15uHvIt+Y8vEf7N7fWAU" crossorigin="anonymous"> <!-- ------------ Only edit title, subtitle & author above this ------------ --> --- name: intro # Introduction Today, we will talk about various elements of a programming language and see how they are realized in R. # Contents - **variables and their types** - **operators** - vectors - numbers as vectors - strings as vectors - matrices - lists - data frames - objects - repeating actions: iteration and recursion - decision taking: control structures - functions in general - variable scope - core functions --- name: variables # Variables - Creating a variable is simply assigning a name to some structure that stores data... ``` r 7 + 9 a <- 7 a b <- 9 b c <- a + b c ``` ``` ## [1] 16 ## [1] 7 ## [1] 9 ## [1] 16 ``` --- # Variables cted. We are not constrained to numbers... ``` r text1 <- 'a' text2 <- 'qwerty' text1 text2 ``` ``` ## [1] "a" ## [1] "qwerty" ``` # Is `<-` equivalent to `=`? Which one shall I use? - `val <- 3`, `val = 3` and `3 -> val` are three equivalent ways of assigning in R, But, it's best to use only `<-` to avoid possible confusion. The equal sign `=` should be used when setting function arguments ie; `f(a = 3)`. --- name: variables_naming # Variables — naming conventions - How to write variable names? - What is legal/valid? - What is a good style? -- A syntactically valid name consists of letters, numbers and the dot or underline characters and starts with a letter or the dot not followed by a number. -- Names such as ".2way" are not valid, and neither are the so-called *reserved words*. -- # Reserved words - `if, else, repeat, while, function, for, in, next, break, TRUE, FALSE, NULL, Inf, NaN, NA, NA_integer_, NA_real_, NA_complex_, NA_character_` - and you also **cannot** use: `c, q, t, C, D, I` - and you **should not** use: `T, F` --- name: variables_good_style # Variables — good style - So, how to name variables? - make them informative, e.g. `genotypes` instead of `fsjht45jkhsdf4`, -- - use consistent notation across your code – the same *naming convention*, -- - camelNotation vs. dot.notation vs. dash_notation, -- - do not `give_them_too_long_names`, -- - in the dot notation avoid `my.variable.2`, use `my.variable2` instead, -- - there are certain customary names: - `tmp` - for temporary variables; - `cnt` for counters; - `i,j,k` within loops, - `pwd` - for password... --- name: data_types_1 # Variables have types A *numeric* stores numbers of different *types*: ``` r x <- 41.99 # assign 41.99 to x typeof(x) ``` ``` ## [1] "double" ``` --- name: class_type_mode # Classes, types, and modes - `class` what type of object is it for R, - `typeof()` what R thinks it is, - `mode()` how S language would see it (backward compatibility), - `storage.mode()` how is it stored in the memory; useful when talking to C or Java, ``` r x <- 1:3 class(x) typeof(x) mode(x) storage.mode(x) ``` ``` ## [1] "integer" ## [1] "integer" ## [1] "numeric" ## [1] "integer" ``` --- name: type_casting # Type casting By default, any *numeric* is stored as *double* ! ``` r y=12 # now assign an integer value to y class(y) # still numeric typeof(y) # an integer, but still a double! ``` ``` ## [1] "numeric" ## [1] "double" ``` But we can explicitly **cast it** to integer: ``` r x <- as.integer(x) # type conversion, casting typeof(x) class(x) is.integer(x) ``` ``` ## [1] "integer" ## [1] "integer" ## [1] TRUE ``` > We need **casting** because sometimes a function requires data of certain type! --- name: casting2 # More on type casting Be careful when casting! ``` r pi <- 3.1415926536 # assign approximation of pi to pi pi pi <- as.integer(pi) # not-so-careful casting pi pi <- as.double(pi) # trying to rescue the situation pi ``` ``` ## [1] 3.141593 ## [1] 3 ## [1] 3 ``` Casting is not rounding! ``` r as.integer(3.14) as.integer(3.51) ``` ``` ## [1] 3 ## [1] 3 ``` --- name: ceiling_floor # Ceiling, floor and a round corner ``` r floor(3.51) # floor of 3.51 ceiling(3.51) # ceiling of 3.51 round(3.51, digits=1) # round to one decimal ``` ``` ## [1] 3 ## [1] 4 ## [1] 3.5 ``` --- name: coercion # What happens if we cast a string to a number ``` r as.numeric('4.5678') as.double('4.5678') as.numeric('R course is cool!') ``` ``` ## [1] 4.5678 ## [1] 4.5678 ## [1] NA ``` --- name: some_special_values # Special values ``` r -1/0 # Minus infinity 1/0 # Infinity ``` ``` ## [1] -Inf ## [1] Inf ``` and also: ``` r 112345^67890 # Also infinity for R 1/2e78996543 # Zero for R Inf - Inf # Not a Number ``` ``` ## [1] Inf ## [1] 0 ## [1] NaN ``` --- name: logical_type # Logical type ``` r a <- 7 > 2 b <- 2 >= 7 a b class(a) typeof(a) ``` ``` ## [1] TRUE ## [1] FALSE ## [1] "logical" ## [1] "logical" ``` R has three logical values: TRUE, FALSE and NA. ``` r x <- c(TRUE, FALSE, NA) names(x) <- as.character(x) and_truth_table <- outer(x, x, "&") # AND table ``` | |TRUE |FALSE |NA | |:-----|:-----|:-----|:-----| |TRUE |TRUE |FALSE |NA | |FALSE |FALSE |FALSE |FALSE | |NA |NA |FALSE |NA | --- name: logical_cted # Logical type cted. ``` r x <- TRUE x x <- T # also valid x is.logical(x) typeof(x) ``` ``` ## [1] TRUE ## [1] TRUE ## [1] TRUE ## [1] "logical" ``` - Observe that in R the logical type is also a numeric! ``` r x <- TRUE y <- FALSE x + y 2 * x x * y ``` ``` ## [1] 1 ## [1] 2 ## [1] 0 ``` --- name: logical_joke # A trap set up for you Never ever use variable names as T or F. Why? ``` r F <- T T F ``` ``` ## [1] TRUE ## [1] TRUE ``` Maybe applicable in politics, but not really in science... --- name: character_type # Character type It is easy to work with characters and strings: ``` r character <- 'c' text <- 'This is my first sentence in R.' text character class(character) typeof(text) # also of 'character' type ``` ``` ## [1] "This is my first sentence in R." ## [1] "c" ## [1] "character" ## [1] "character" ``` --- name: character_type_2 # Character type ``` r number <- 3.14 number.text <- as.character(number) # cast to char number.text class(number.text) as.numeric(number.text) # and the other way round ``` ``` ## [1] "3.14" ## [1] "character" ## [1] 3.14 ``` --- name: basic_string_ops # Basic string operations ``` r text1 <- "John had a yellow " text2 <- "submarine" result <- paste(text1, text2, ".", sep='') result sub("submarine", "cab", result) substr(result, start=1, stop=5) ``` ``` ## [1] "John had a yellow submarine." ## [1] "John had a yellow cab." ## [1] "John " ``` --- name: basic_printing # Basic printing ``` r txt <- "blue" (txt <- 'red') val <- 345.78 sprintf("The weight of a %s ball is %g g", txt, val) ``` ``` ## [1] "red" ## [1] "The weight of a red ball is 345.78 g" ``` <!-- --------------------- Do not edit this and below --------------------- --> --- name: end_slide class: end-slide, middle count: false # See you at the next lecture! .end-text[ <p class="smaller"> <span class="small" style="line-height: 1.2;">Graphics from </span><img src="./assets/freepik.jpg" style="max-height:20px; vertical-align:middle;"><br> Created: 31-Oct-2024 • <a href="https://www.scilifelab.se/">SciLifeLab</a> • <a href="https://nbis.se/">NBIS</a> </p> ]