---
title: "R tutorial (Updated)"
output: html_notebook
---
ENSC454		 Snow & Ice   	       

###################################

Lets use R a simple calculator
```{r}
2+2
2+2 * 100
(2+2) * 100
2+2 * 100/10
```

Create objects
```{r}

var1<- c(3, 5, 2, 6, 1, 8, 9, 7)
var2<- c(2010, 2014, 2016, 2011, 2015, 2020, 2021)  

```

Loading Input data files
```{r}
#data <- read.csv("sample_weather_data.csv",header = TRUE)[-1,]
data <- read.csv("sample_weather_data.csv", header = TRUE)
head(data)
data.AT <- data$AirTC
data.RH <- data$RH
time <- data$Timestamp
newdata <- data.frame(data.AT, data.RH)
head(newdata)
newdataT <- data.frame(time, data.RH)
head(newdataT, n=5) # shows the data for the first five indexes
tail(newdataT, n=2) # shows the data for the last two indexes 
```

Data can be accessed by index. Lets use square brackets to subset data (sometimes also called “slicing”). The generic format is dat[row_numbers,column_numbers].
```{r}
#data[, 2]
data.AT[1:12]
```

Writing output files
```{r}

# Write to a file, suppress row names
write.csv(newdataT, "newdataT.csv", row.names=FALSE)

# Same, except that instead of "NA", output blank cells
write.csv(newdataT, "newdataT.csv", row.names=FALSE, na="")

# Use tabs, suppress row names and column names
write.table(newdataT, "newdataT.txt", row.names=FALSE, col.names=FALSE,append = FALSE, sep = " ", dec = ".")
```

Data is collected on 15 minutes time interval so we will need to convert it to daily. Total length of data is 11712.
```{r}
4 * 24
11712/96
```

Data conversion
```{r}
# create sequence of data
length(data.AT)
#str(data.AT)
dataAT_mat<- matrix(data.AT, nrow=96, ncol=122)#matrix conversion
dataRH_mat<- matrix(data.RH, nrow=96, ncol=122)#matrix conversion

dataAT_daily<- matrix(nrow=122, ncol=0)#emptry matrix for new data
dataRH_daily<- matrix(nrow=122, ncol=0)#emptry matrix for new data

for(i in 1:122){
dataAT_daily[i]<- mean(dataAT_mat[,i])
dataRH_daily[i]<- mean(dataRH_mat[,i])
} 

dataAT_daily # New data converted into daily

```

Save new data into .csv file
```{r}
dates = seq(as.Date('2010-09-01'),as.Date('2010-12-31'),by = 1) # create daily timestamps

dataAT_daily1 <- data.frame(dates, dataAT_daily)
format(dataAT_daily1, digits=2, nsmall=2)
write.csv(dataAT_daily1, "daily-data_AT.csv", row.names=FALSE)

```

Lets extract snow depth variable and find outliers
```{r}
data.SD <- data$SnowDepth
head(data.SD)
elem_pos = which(data.SD > 100)
elem_pos
data.SD[1013]
data.SD[11370]
```
Replace outliers with NA
```{r}
for(i in elem_pos){
data.SD[i]<- NA

} 
data.SD[1013]
data.SD[11370]
```

Plotting
```{r}
#plot(x, y) 
plot(x=dates, y=dataAT_daily, type = "l", col = "1", main = "Daily Data", ylab = "AT", xlab = "Days")
```

Linear Model.
```{r}
trnd <- lm(dataAT_daily~dates)
plot(x=dates, y=dataAT_daily, type = "l", col = "1", main = "Daily Data", ylab = "AT", xlab = "Days")
abline(trnd, col="red")
```

Extract info from linear model e.g. slope
```{r}
summary(trnd) # Slope and intercept
```

```{r}
slope <- trnd$coefficients[2]
slope * 122 # Total trend over 122 days
```

Some Basin Statistics
```{r}
CR <- cor(dataAT_daily,dataRH_daily) # Correlation
VR <-var(dataAT_daily) # Variance
CR
SD <-sd(dataAT_daily)
MN<- mean(dataAT_daily)
CV<- SD/MN # Coefficient of Variation
CV
```

