# Introduction to R

R as a programming language.

# Getting started with R in a few steps

- try out the console

## Arithmetrics with R 

- Basic arithmetic operations：`+` (addition), `-` (substraction), `*` (multiplication), `/` (division), `^` (exponentiation)

```{r}
3+5
14/2
```

- Basic arithmetic **functions**
    - logarithmus and exponentials: `log2(x), log10(x), exp(x)`
    - Others: `abs(x)`, `sqrt(x)`

```{r}  
log2(6)  
sqrt(36)
```
> Getting help

```{r}
?sqrt
```


## Parameter (參數) 與 Argument (引數). 
  - argument: 【呼叫者】傳給【被呼叫者】的「值」(value)
  - parameter: 【被呼叫者】用來接收【呼叫者】傳來引數的「變數」(variable)

自然語言亂入：`欸()`, `叫(x, num, pitch)`. 


> `叫(啊, num = 3, pitch = 'high')`


```{r}
head(cars, n=10)

```


# Data types

- Basic *data types*: **numeric, character, logical**
- Supported *data structure*: **vector, matrix, factor, data frame** 




# Variable and value assignment 變數與賦值 `<-`

```{r}
# numeric variable
my_age <- 40
# character variable
my_name <- "shukai"
# logical variable (e.g., are you a student?) y/n
is_student <- TRUE
```



# Install packages

```{r}
#install.packages("tidyverse")
```



```{r}
#install.packages(c("quantmod", "dygraphs"))

```


```{r}
library(quantmod)
library(dygraphs)
```

- Let's check Google stock since the 2008

```{r}
g_stock_prices <- getSymbols("GOOG", src = "yahoo", from = "2008-01-01", auto.assign = FALSE)
chartSeries(g_stock_prices)
```

```{r}
dygraph(g_stock_prices[,c("GOOG.Close")], main = "Google Stock Price Starting in 2008")

```
