---
title: "Introduction to R"
author: "Ic3fr0g"
date: '`r Sys.Date()`'
output:
  html_notebook:
    code_folding: show
    fig_caption: yes
    fig_height: 4.5
    fig_width: 7
    highlight: tango
    number_sections: yes
    theme: cosmo
    toc: yes
  html_document:
    code_folding: show
    fig_caption: yes
    fig_height: 4.5
    fig_width: 7
    highlight: tango
    number_sections: yes
    theme: cosmo
    toc: yes
---


# What is R ?

R is a free programming language and software environment for statistical computing and graphics. The R language is widely used among statisticians and data miners for developing statistical software and data analysis.

R and its libraries implement a wide variety of statistical and graphical techniques, including linear and nonlinear modeling, classical statistical tests, time-series analysis, classification, clustering, and others. R is easily extensible through functions and extensions, and the R community is noted for its active contributions in terms of packages.

R isn't used as general programming language. It sees more usage in Data Analysis and Data Visualisation primarily because of the powerful libraries behind it.


# Install R and RStudio

- [Download and install R](https://ftp.iitm.ac.in/cran/)
- [Download and install RStudio](https://www.rstudio.com/products/rstudio/download/)


# Scalars {.tabset .tabset-fade .tabset-pills}

## Data Types
R works with numerous data types. Some of the most basic types to get started are:

- Text (or string) values are called **characters**.
- Decimals values like `4.5` are called **numerics**.
- Natural numbers like `4` are called **integers**. Integers are also numerics.
- Boolean values (`TRUE` or `FALSE`) are called **logical**.

## Strings
As is customary with every new language one learns, let's learn how to print **"Hello world!"**:
```{r}
"Hello world!"
```

## Arithmetic
In its most basic form, R can be used as a simple calculator. Consider the following arithmetic operators:

- Addition: +
- Subtraction: -
- Multiplication: *
- Division: /
- Exponentiation: ^
- Modulo: %%
For example:
```{r}
# Remainder when 3 is divided by 2
3 %% 2
```
Another interesting thing to point out is that it inherently follows BODMAS:
```{r}
# Inherently follows BODMAS
6 - 7 * 2 / 1 + 3
```

## Logical Values
There are 3 logical operators:

- `&`, `&&` - and
- `|`, `||` - or
- `!` - not

The (logical) comparison operators known to R are:

- `<` for less than
- `>` for greater than
- `<=` for less than or equal to
- `>=` for greater than or equal to
- `==` for equal to each other
- `!=` not equal to each other

Most programming languages refer to logical values as **boolean values**. Some expressions can return `True` or `False` values:
```{r}
# AND
1 & 0
```
```{r}
# OR
0 | 1
```
```{r}
# NOT
!0
```
Here's an example of the comparison logical operator:
```{r}
3 < 4
```
The `==` sign checks for equivalence of an expression:
```{r}
# == sign checks for equivalence
2 + 2 == 5
```
Shorthand for `TRUE` and `FALSE` are given as `T` and `F` respectively:
```{r}
T == TRUE
```


# Variables {.tabset .tabset-fade .tabset-pills}

## Assignment
As in other programming languages, you can store values into a variable to access it later:
```{r}
# Assign the value 42 to x
x <- 42
# Print out the value of the variable x
x
```
Another way to assign a variable is by calling the `assign(...)` function:
```{r}
# Call the assign function
assign('this_var_x',43210)
# Display 'this_var_x'
this_var_x
```

## Arithematic
Variable arithmetic also works but notice that it doesn't change the original value of `x`:
```{r}
# Divide x by 21
x/21
# Print x
x
```
To change the value of `x`, we'd have to do the following:
```{r}
# Save the result of x/21 to a variable x
x <- x/21
# Print x
x
```
Let's assume we have 3 variables where each variable here denotes the number of fruits we bought at the market:
```{r}
# Fruits bought at the market
apples <- 5
oranges <- 6
tomatoes <- "ten"
```
Let's find out the sum of the apples and oranges bought collectively:
```{r}
# Sum of apples and oranges
apples + oranges
```
What about the sum of the apples and tomatoes ?
```{r,error=T}
apples + tomatoes
```
**This doesn't work as expected because the two variables(`apples` and `tomatoes`) are of different datatypes(`integer` and `character`).**

## Logical
Variables can be compared to scalars:
```{r}
# Are the number of apples less than 2?
apples < 2
```
Variables can be compared to other vectors as well:
```{r}
# Comparing vectors
apples == oranges
```
We can introduce complex logical expressions by linking them with either of the three logical operators(`!`,`|`,`&`):
```{r}
apples > 2 & oranges < 7
```


# Introduction to Functions and Helpers {.tabset .tabset-fade .tabset-pills}

## Examples of functions
Functions exist to perform repeated tasks. You call a function by typing its name, followed by one or more arguments to that function in parenthesis. Let's see an example of the `sum(...)` function:
```{r}
# Sum of 1, 3, 5
sum(1,3,5)
```
Another example:
```{r}
# Sum of apples and oranges
sum(apples, oranges)
```
Some arguments have names. For example, to repeat a value 3 times, you would call the `rep(...)` function and provide its `times` argument:
```{r}
# Repeats "FIRE" 3 times
rep("FIRE", times = 3)
```
Most mathematical functions, like `sqrt(...)`, have well defined functions:
```{r}
# Square root of 16
sqrt(16)
```

## Helpers
`help(...)` brings up help for the given function. Try displaying help for the sum function:
```{r}
help(sum)
# We can also bring up the help file in this way
?sum
```
`example(...)` brings up examples of usage for the given function. Try displaying examples for the min function:
```{r}
example(sum)
```

## Tests
One of the most important helper functions used is `class(...)`. It helps us to determine the datatype of the variable:
```{r}
# Returns the class of the object
class(apples)
```
Another way to determine the class of an object follows:
```{r}
# Returns a boolean value
is.numeric(tomatoes)
```


# Miscellaneous

A **syntactically valid variable name** consists of letters, numbers and the dot or underline characters and starts with a letter or the dot not followed by a number. Names such as ".2way" are not valid, and neither are the reserved words. See `?make.names`.

Commands are separated either by a semi-colon (`;`), or by a newline. Elementary commands can be grouped together into one compound expression by braces (`{` and `}`). Comments can be put almost anywhere, starting with a hashmark (`#`), everything to the end of the line is a comment. Finally, `print(...)` prints a message.
```{r}
# Commands seperated by ;
x <- 1; y <- 2; print(x + y)
```

