Coding basics

Jeff Stevens

2023-01-27

Review: Mental model of RStudio

Terminology

Functions

Perform computations and create output

[1] "2023-03-16"
[1] "/media/jstevens/data/jstevens/OneDrive/active_sync/projects/dpavir_2023/slides"

Arguments

Specifications for information needed by functions

read.csv("mypath/myfile.csv")
dplyr::filter(mydata, mycolumn == "Sophomore")
plot(x, y)
write.csv(mydata, "mypath/myfile.csv")
read.csv(file = "mypath/myfile.csv")
dplyr::filter(.data = mydata, mycolumn == "Sophomore")
plot(x = x, y = y)
write.csv(x = mydata, file = "mypath/myfile.csv")

Note

If you give argument values in correct order, you don’t need to include argument names. But it’s usually a good idea anyway.

Objects

Variables created to store information

mydata <- read.csv(file = "mypath/myfile.csv")
trimmed_data <- dplyr::filter(.data = mydata, mycolumn == "Sophomore")
myplot <- plot(x = x, y = y)

Assignment

Assign a value or set of values to an object

# the best way
x <- 9
# avoid this
y = 10
# definitely don't do this
11 -> z

Note

You can assign multiple objects at the same time:

# chain assignments of the same value to different objects
a <- b <- c <- 0

Assignment

Viewing object contents while assigning

x <- 9  # assign value 9 to object x
x  # print contents of object x to console
[1] 9
(x <- 9)  # add parentheses to print to console when assigning
[1] 9

Mental model of coding

Coding style

mean1=mean (x[1,4:10],na.rm=T)+0.5
mean1 <- mean(x[1, 4:10], na.rm = TRUE) + 0.5

Coding style

The tidyverse style guide

  • Use <- as assignment operator

  • Use space between operators (*, =, ==) and after commas

  • Write out TRUE and FALSE

  • Do not use space between function and parentheses

  • Use indents to separate nested components (Ctrl+I)

  • Use ", not ', for quoting text unless it already contains double quotes

Naming things

Naming things

Core principles for naming objects, data columns, files, folders

  • Be nice to machines

  • Be nice to humans

  • Make sorting and searching easy

Be nice to machines

  • avoid spaces, special characters, and accented characters
    • my_file.R not My filé$.R
  • avoid case sensitivity
    • foo.R and Foo.R
  • use consistent, searchable text chunks
    • expt1_cond2_subj114.csv
  • can’t start with a number

Be nice to humans

  • be descriptive (not x) but not too descriptive (this_is_my_object)
  • separate words (preferably using snake_case)
  • avoid capital letters (case matters: a ≠ A)
  • use human readable names that contain content (slugs)
    • prelim_analysis_expt1.R

Make sorting and searching easy

  • use ISO 8601 standard for date YYYY-MM-DD
  • no, really—always use ISO 8601 standard for date!!
  • use ISO 8601 dates before or after slugs
    • 2021-04-06_prelim_analysis_expt1.Rmd
    • prelim_analysis_expt1_2021-04-06.Rmd
  • use padded numbers as prefixes
    • 01_preface.Rmd
    • 02_introduction.Rmd

Mental model of coding

Let’s code!

Coding basics [Rmd file]