## ------------------------------------------------------
a <- 0

if (a != 0) {
  print(1/a)
} else{
  print("No reciprocal for 0.")
}


## ------------------------------------------------------
library(dslabs)
murder_rate <- murders$total / murders$population*100000


## ------------------------------------------------------
ind <- which.min(murder_rate)

if (murder_rate[ind] < 0.5) {
  print(murders$state[ind]) 
} else{
  print("No state has murder rate that low")
}


## ------------------------------------------------------
if (murder_rate[ind] < 0.25) {
  print(murders$state[ind]) 
} else{
  print("No state has a murder rate that low.")
}





#### Quiz (Bonus: 1 points)
# Write a if/else code to check if a number is odd ( %%2!=0)
## Example Output: 2 is even.   




## ------------------------------------------------------
a <- 0
ifelse(a > 0, 1/a, NA)


## ------------------------------------------------------
a <- c(0, 1, 2, -4, 5)
result <- ifelse(a > 0, 1/a, NA)



## ------------------------------------------------------
no_nas <- ifelse(is.na(na_example), 0, na_example) 
sum(is.na(no_nas))




## ------------------------------------------------------
z <- c(TRUE, TRUE, FALSE)
any(z)
all(z)


################################# 
#### For Loops 
############################
------------------------------------------------------
for (i in 1:5) {
  print(i)
}


## ------------------------------------------------------
m <- 25
s_n <- vector(length = m) # create an empty vector
for (n in 1:m) {
  s_n[n] <- sum(1:n)
}

############### 
#### Exercise: 
#### 
################
ID<-paste("ID-", c(12, 54, 22, 31), sep="")
ID
strsplit(ID[[1]], split="-")
strsplit(ID[[1]], split="-")[2]

#### write a for loop to get the numbers from the character string ID. 



####### 

## ------------------------------------------------------
avg <- function(x){
  s <- sum(x)
  n <- length(x)
  s/n
}


## ------------------------------------------------------
x <- 1:100
identical(mean(x), avg(x))


## ------------------------------------------------------
s <- 3
avg(1:10)
s


## ---- eval=FALSE---------------------------------------
## my_function <- function(VARIABLE_NAME){
##   perform operations on VARIABLE_NAME and calculate VALUE
##   VALUE
## }


## ------------------------------------------------------
avg <- function(x, arithmetic = TRUE){
  n <- length(x)
  ifelse(arithmetic, sum(x)/n, prod(x)^(1/n))
}


## ------------------------------------------------------
compute_s_n <- function(n) { 
  sum(1:n)
}





## ----sum-of-consecutive-squares, out.width="50%", echo=FALSE----
n <- 1:m
plot(n, s_n)


## ------------------------------------------------------
x <- 1:10
sqrt(x)
y <- 1:10
x*y

############### 
#### Exercise: 
#### 
################
ID<-paste("ID-", c(12, 54, 22, 31), sep="")
ID
strsplit(ID[[1]], split="-")
strsplit(ID[[1]], split="-")[2]

#### write a function to get the numbers from a character 
 



## ------------------------------------------------------
x <- 1:10
sapply(x, sqrt)



############### 
#### Exercise: 
#### 
################
ID<-paste("ID-", c(12, 54, 22, 31), sep="")
ID
strsplit(ID[[1]], split="-")
strsplit(ID[[1]], split="-")[2]

#### write a function to get the numbers from a character 
#### Use sapply for a character vector


############ apply 
mat<-matrix(1:20, ncol=2)
cMean<-apply(mat, 2, mean)
cMean
rMean<-apply(mat, 1, mean)
rMean



###########################################################################  
#### Exercise 
#  1. Generate 30 random numbers from normal distribution 
#  2. Randomly assign 15 to the control group and 15 to the treatment group 
#  3. Perfom t test and report p value
#############################################################################



   