1 Set Up

  1. Install R, then R Studio

https://posit.co/download/rstudio-desktop/

  1. Reading up on the help files from official R website

    • The R manuals https://www.r-project.org
    • there are many other websites that might be easier to read
    • The help() function and ? help operator in R provide access to the documentation pages for R functions, data sets, and other objects, both for packages in the standard R distribution and for contributed packages.

2 Coding Best Practices

http://adv-r.had.co.nz

2.1 Notation and Naming

  • File names should be meaningful and informative

  • Variable and function names should be lowercase

    • use underscore

2.2 Syntax

  • Place spaces around operators

Let me begin by introducing basic math operations.

3 Basic Data Types

3.0.1 What Are R Data Types?

R data types are used to specify the kind of data that can be stored in a variable.

For effective memory consumption and precise computation, the right data type must be selected.

Each R data type has its own set of regulations and restrictions.

Basic data types in R can be divided into the following types:

  • numeric - (10.5, 55, 787)

  • integer - (1L, 55L, 100L, where the letter “L” declares this as an integer

  • complex - (9 + 3i, where “i” is the imaginary part)

    • Popular in physics, but not in business/economics
  • character (a.k.a string) - (“k”, “R is exciting”, “FALSE”, “11.5”)

    • Addresses or names, states or countries
  • logical (a.k.a boolean) - (TRUE or FALSE)

    • comparison, TRUE is usually 1 and FALSE is usually 0
    ?datasets()
    df <- mtcars
    df$mpg2 <- as.integer(df$mpg)
    df$mpg3 <- as.numeric(df$mpg)
    
    df <- as.data.frame(Titanic)
    str(df)
    ## 'data.frame':    32 obs. of  5 variables:
    ##  $ Class   : Factor w/ 4 levels "1st","2nd","3rd",..: 1 2 3 4 1 2 3 4 1 2 ...
    ##  $ Sex     : Factor w/ 2 levels "Male","Female": 1 1 1 1 2 2 2 2 1 1 ...
    ##  $ Age     : Factor w/ 2 levels "Child","Adult": 1 1 1 1 1 1 1 1 2 2 ...
    ##  $ Survived: Factor w/ 2 levels "No","Yes": 1 1 1 1 1 1 1 1 1 1 ...
    ##  $ Freq    : num  0 0 35 0 0 0 17 0 118 154 ...

4 Arithmetic Operations

4.1 Addition Operation

2 + 2 # addition
## [1] 4

4.2 Subtraction Operation

5 - 4
## [1] 1

4.3 Multiplication Operation

2 * 3
## [1] 6

4.4 Division Operation

8/3
## [1] 2.666667
# Plot data -------------------