1 Set Up

  1. Installed R, then R studio. https://posit.co/download/rstudio-desktop/
  2. Reading up on the help files from official R website.
    • There are many other websites that may 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.

      • Many other websites may be easier to read.

2 Coding Best Practices

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

2.1 Notation and naming

  • File names should be meaningful/informative.

  • Variable and function names should be lowercase. Use underscore ( _ ) to separate words.

  • Where possible, avoid using names of existing functions and variables.

2.2 Syntax

  • Place spaces around all infix operators (=, +, -, <-, etc.).

2.3 Organisation

2.3.1 Commenting guidelines

Comment your code. Each line of a comment should begin with the comment symbol and a single space: #. Comments should explain the why, not the what.

Use commented lines of - and = to break up your file into easily readable chunks.

3 Basic 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.

https://www.geeksforgeeks.org/r-data-types/

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)

  • character (a.k.a. string) - (“k”, “R is excited”, “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.
  • complex (9 + 3i, where “i” is the imagery part)

    • Popular in physics, but not in business/economics.
?datasets()
df <- mtcars
?as.integer
df$mpg2 <- as.integer(df$mpg)
df$mpg3 <- as.numeric(df$mpg2)

df2 <- as.data.frame(Titanic)

str(df)
## 'data.frame':    32 obs. of  13 variables:
##  $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
##  $ cyl : num  6 6 4 6 8 6 8 4 4 6 ...
##  $ disp: num  160 160 108 258 360 ...
##  $ hp  : num  110 110 93 110 175 105 245 62 95 123 ...
##  $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
##  $ wt  : num  2.62 2.88 2.32 3.21 3.44 ...
##  $ qsec: num  16.5 17 18.6 19.4 17 ...
##  $ vs  : num  0 0 1 1 0 1 0 1 1 1 ...
##  $ am  : num  1 1 1 0 0 0 0 0 0 0 ...
##  $ gear: num  4 4 4 3 3 3 3 4 4 4 ...
##  $ carb: num  4 4 1 1 2 1 4 2 2 4 ...
##  $ mpg2: int  21 21 22 21 18 18 14 24 22 19 ...
##  $ mpg3: num  21 21 22 21 18 18 14 24 22 19 ...
2 > 3
## [1] FALSE

Let me begin by introducing basic math operations.

4 Arithmetic Operation

##Addition Operation

2 + 2 # addition
## [1] 4

4.1 Subtraction Operation

5 - 4
## [1] 1

4.2 Multiplication Operation

2 * 3
## [1] 6

4.3 Division Operation

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