1 Set Up

  1. Install R, then R Studio. https://posit.co/download/rstudio-desktop/

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

2 Coding Best Practices

Good coding style is like using correct punctuation. You can manage without it, but it sure makes things easier to read. As with styles of punctuation, there are many possible variations. The following guide describes the style that I use (in this book and elsewhere). It is based on Google’s R style guide, with a few tweaks. You don’t have to use my style, but you really should use a consistent style. 

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

2.1 Notation and naming

  • Files 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 - 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

Different forms of data that can be saved and manipulated are defined and categorized using data types in computer languages, including R. Each R data type has unique properties and associated operations.

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

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

  • 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.
  • complex - (9 + 3i, where “i” is the imaginary part)

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


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

4 Data Structures

https://swcarpentry.github.io/r-novice-inflammation/13-supp-data-structures.html

Let me begin by introducing basic math operations.

5 Arithmetic Operations

https://www.codecademy.com/resources/docs/r/operators

5.1 Addition Operation

2+2 # addition
## [1] 4

5.2 Subtraction Operation

5 - 4
## [1] 1

5.3 Multiplication Operation

2 * 3
## [1] 6

5.4 Division Operation

8/3
## [1] 2.666667




# Plot data ---------------------------

hist(mtcars$mpg)