---
title: "class_demo"
author: "Muhammad"
date: "March 11, 2019"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

Write a function which takes a vector as input and replace every even position element by -1<br>
Input : c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)<br>
Output : [0, -1,  2, -1,  4, -1,  6, -1,  8, -1]
```{r}
a <- seq(0,9)
evenReplace <- function(a){
  a[seq(2,length(a), by= 2)] <- -1
  return(a)
}
evenReplace(a)
```

Write a function which takes a vector as input and changes all NA valuse by 0<br>
Input : [1, 2, 3, 4, NA, 5, NA, 6]<br>
Output : [1, 2, 3, 4, 0, 5, 0, 6]
```{r}
v <- c(1, 2, 3, 4, NA, 5, NA, 6) 
na2z <- function(v){
  v[is.na(v)] <- 0
  return(v)
}
na2z(v)
```

Write a function which takes a vector as input and all non NA values<br>
Input : [1, 2, 3, 4, NA, 5, NA, 6]<br>
Output : [1, 2, 3, 4, 5, 6]
```{r}
v <- c(1, 2, 3, 4, NA, 5, NA, 6) 
rmNa <- function(v){
  v <- v[!is.na(v)]
  return(v)
}
rmNa(v)
```

Generate the list ['z','z',1,2,3,4,5,8,10,12]
```{r}
a <- c(rep('z',2),seq(1,5),seq(8,12,by = 2))
print(a)
```

Write a function to reverse a vetor
```{r}
u <- seq(0,9)
reverse <- function(u){
  return(u[seq(length(u),1)])
}
reverse(u)
```

Write a function to recognize palindrome
```{r}
v <- c("a","b","b","a")
u <- c("a","b","a","a")
plain <- function(v){
  z <- v[seq(length(v),1)]
  if (sum(v == z) == length(v)){return(TRUE)}
  else(return(FALSE))
}
plain(v)
plain(u)
```

Importing weather data
```{r}
d <- read.csv('Indianapolis-Weather-Station-USW00093819-1950-2018.csv', na.strings = c("M","T"))
d$year <- substr(d$Date,1,4)
yearSummary <- data.frame(year = unique(d$year))
```

Top 10 average coldest years (arrange years based on average min temperature)
```{r}
yearSummary$tmin <- tapply(d$TMIN,d$year,mean)
head(yearSummary[order(yearSummary$tmin),c("year", "tmin")], n=10) 
```

Top 10 average warmest years (arrange years based on average max temperature)
```{r}
yearSummary$tmax <- tapply(d$TMAX, d$year, mean)
head(yearSummary[order(yearSummary$tmax, decreasing = TRUE), c("year", "tmax")], n=10)
```

Effect of precipitation on min Temperature

```{r}
yearSummary$minT <- tapply(d$TMIN, d$year, function(x){min(x,na.rm = TRUE)})
yearSummary$prcpAtMinT <- tapply(d$TMIN, d$year, function(x){d$PRCP[which.min(x)]})

plot(yearSummary$minT, yearSummary$prcpAtMinT, pch= 8, xlab = "Min Temperature", ylab = "Precipitation")
```

