---
title: "Siraj's R Notebook"
output: html_notebook
---

Lets clear all objects includes hidden objects.
```{r}
rm(list = ls(all.names = TRUE)) 
gc()

```

Check in which directory you are working.
```{r}
getwd()
```

Todays date.
```{r}
format(strptime("21/1/20", "%d/%m/%y"), "%a %b %d %Y")
```

Compound interest Solution.
```{r}
# Investment
P<-2000

# Interest rate
r<-0.03

# Term
n<-1:30

total.ammount<- P * (1 + r)^n
total.ammount
```

Check if there are store objects.
```{r}
objects()
```

Exmaples from lecture today.
```{r}
numbers1 <- c(2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41)
numbers2 <- c(43, 47, 59, 67, 71, 73, 79, 83, 89, 97, 103, 107, 109)
numbers12 <- c(numbers2, numbers1)
numbers12
```


```{r}
numbers123 <- c(numbers1, numbers2,1:6)
numbers123
```

We can extract more than one element at a time. For example, the third, sixth, and seventh elements of numbers123.
```{r}
length(numbers123)

numbers123[c(3,5,8)] # use -c if you want to avoid these elemements
```


```{r}
numbers123[3:7]
```


```{r}
zz <-numbers123[3:7]
zz^3
```


```{r}
seq(10, 100, by = 3)
```


```{r}
rep(3.5, 12)
rep(seq(20, 30, by = 2), 2)
```


```{r}
rep(c(1, 4), c(3, 2))
rep(c(1, 4), each = 3)
rep(1:4, rep(2, 4))
```

The sample() function allows us to simulate things like the results of the repeated tossing of a 6-sided die.
```{r}
ss <- sample(1:100, size = 50, replace = TRUE) # an imaginary die is tossed 8 times

```


```{r}
students <- c("alpha", "beta", "gamma")
students
more.students <- c(students, "tango")
more.students
substr(more.students, 1, 5) #print the first two letters of each color use
```


```{r}
paste("marks of ", more.students, "are" )
## [
```


```{r}
paste("marks of ", more.students, "are" , sep = ",")
paste("marks of ", more.students, "are" , collapse = ", ")
```

Factors can be an efficient way of storing character data when there are repeats among the vector elements.
```{r}
grp <- c("male", "female", "male", "female", "control", "treatment","control", "treatment")
grp
grp <- factor(grp)
grp
```

The levels() function can be used to change factor labels as well
```{r}
levels(grp)
levels(grp)[1] <- "test"

grp[3]
```


```{r}
m <- matrix(1:10, nrow = 10, ncol = 10)
m
```


```{r}
m[,7]
```


```{r}
a <- array(15,c(3, 4)) #the dimensions were specified in a vector c(3, 4, 2)
a

a <- array(20, c(3, 4,6)) #the dimensions were specified in a vector c(3, 4, 2)
a
```


```{r}
numbers123

x <- numbers123
length(x)
mean(x)
var(x)
sum( (x - mean(x))^2 ) / 31 # variance formula
```

```{r}
rm(list = ls(all.names = TRUE)) 
gc()
objects()

```

First part of the lab ends here.
####################################################

Lab 3, Part (b)
Lets load the real scientific data in different format.  

```{r}
file.show("./Fraser_Hope.txt")
```


```{r}
wt<-read.csv("./river_WT_spawn_data.csv", header=TRUE) 
head(wt)

dc<-read.table("./Fraser_Hope.txt", header=TRUE)
head(dc)
```

```{r}

```


```{r}

dc$JUL
plot(wt$WT)
rmse()
```


```{r}
obs<-read.table("./observed_mean_temperature_precipitation_1950-2012.txt", header=TRUE) 
mod<-read.table("./model_mean_temperature_precipitation_1950-2012.txt", header=TRUE)
head(obs)
head(mod)
```




