Day 1 & 2 Questions

RAdelaide 2024

Author
Affiliation

Dr Stevie Pederson

Black Ochre Data Labs
Telethon Kids Institute

Published

July 9, 2024

Questions

  1. Are there rules for naming R objects?
  • Names cannot include spaces!

  • Should start with an alphabetic character

  • Can start with symbols & numbers but that takes special effort and makes your life extremely hard. Don’t do it (!!!) unless you have very, very good reason to

  • Two common naming conventions are

    1. camelCase: first word starts with lower case, subsequent words start with upper
    2. snake_case: All lower case with underscores separating words
  • I’m inconsistent…

  1. How do we select multiple values/columns/rows using []
  • I usually use 1:5 as a simple example
  • This is an integer vector \(\implies\) we can use any integer vector
x <- rnorm(20)
mat <- matrix(x, ncol = 5)
mat
           [,1]       [,2]       [,3]       [,4]       [,5]
[1,] -0.7822772 -0.5983013  0.4705443 -1.7854542  0.7890799
[2,] -0.5489170 -1.1276186  1.2845722  0.7102603  1.6476568
[3,] -0.7892259  0.3790830 -0.4309880 -0.3757379 -0.5893949
[4,] -0.7680385 -1.1002752  1.4052935  0.7397973  0.6613019
mat[,c(1, 3)]
           [,1]       [,2]
[1,] -0.7822772  0.4705443
[2,] -0.5489170  1.2845722
[3,] -0.7892259 -0.4309880
[4,] -0.7680385  1.4052935
  • We can call in any order at all
euro
        ATS         BEF         DEM         ESP         FIM         FRF 
  13.760300   40.339900    1.955830  166.386000    5.945730    6.559570 
        IEP         ITL         LUF         NLG         PTE 
   0.787564 1936.270000   40.339900    2.203710  200.482000 
euro[c(6, 9, 1)]
     FRF      LUF      ATS 
 6.55957 40.33990 13.76030 
  • If we have column (or row) names, can be a character vector
colnames(mat) <- paste0("C", 1:5)
mat
             C1         C2         C3         C4         C5
[1,] -0.7822772 -0.5983013  0.4705443 -1.7854542  0.7890799
[2,] -0.5489170 -1.1276186  1.2845722  0.7102603  1.6476568
[3,] -0.7892259  0.3790830 -0.4309880 -0.3757379 -0.5893949
[4,] -0.7680385 -1.1002752  1.4052935  0.7397973  0.6613019
mat[,c("C1", "C3")]
             C1         C3
[1,] -0.7822772  0.4705443
[2,] -0.5489170  1.2845722
[3,] -0.7892259 -0.4309880
[4,] -0.7680385  1.4052935
  1. Why do we use a character vector using [] but not using select()?
  • R objects must be called without quotations but when calling column/row names we need a character vector, because these values are saved as character vectors
  • The tidyverse has done same really fancy innovation so that we don’t need quotes in their functions. This partially mimics using the list$element_name syntax and effectively considers each list element of a data frame as an R object able to be called by name. This is genuinely confusing to many and has been a complete revolution & paradigm shift over the last few years. However, it does use some really convoluted concepts & fancy code under the hood

Other Comments & Tips

  1. Sorry to hear that some versions of RStudio don’t show a preview of colours in the editor…

  2. Remember that typing code in a script doesn’t execute code. Code must be sent to the Console to make sure all objects are created. It does take some getting used to. Keep going. It’ll become instinctive soon

  3. Is my dark theme in RStudio confusing anyone?