## ------------------------------------------------------
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<-2
print(paste(a, "is even."))


n<-25 
if( n %%2!=0){
	print(paste(n, "is odd."))
}else{
	print(paste(n, "is even."))
}




## ------------------------------------------------------
a <- 2
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(result), 0, result)
no_nas 
sum(is.na(no_nas))


######################## 
## ifelse exercise
########################


########################################################################################
# 1. Read in the experiment-design.tsv on the course website
# 2. Rename the variable: Sample.Characteristic.biopsy.site.
# 3.   change "primary tumor" to "primary"
#           "colorectal cancer metastatic in the liver" to "metastatic"
# 4. Show out tabulate table of the number of observation in each category described in 3.
#########################################################################################

url<-"https://people.stat.sc.edu/hoyen/STAT540/Data/experiment-design.tsv"
dat<-read.delim(file=url, header=T)
head(dat)

tissueType<-dat[,2]
table(tissueType)


####ifelse works for a vector 

tissueType<-ifelse(tissueType=="primary tumor", "primary", tissueType)
tissueType<-ifelse(tissueType=="colorectal cancer metastatic in the liver", "metastatis", tissueType)

#### 




## ------------------------------------------------------
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)
}

plot(1:m, s_n)


############### 
#### Exercise: 
#### 
################
ID<-paste("ID-", c(12, 54, 22, 31), sep="")
ID
strsplit(ID[[1]], split="-")
strsplit(ID[[1]], split="-")[[1]][2]

#### write a for loop to get the numbers from the character string ID. 

x<-vector(length=length(ID))
for(i in 1:length(ID)){
	x[i]<-strsplit(ID[i], split="-")[[1]][2]
}
x


####### 

## ------------------------------------------------------
avg <- function(x){
  s <- sum(x)
  n <- length(x)
  s/n
}

###### 
cumsum<-function(n){
	ans<-n*(n+1)/2
	return(ans)
}




## ------------------------------------------------------
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
## }

myfunc2<-function(x, y){
	ans<-x^2 + y^2
	return(ans)
}



## ------------------------------------------------------
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 
ID<-"ID-242"

grab_num<-function(ID){
	ans<-strsplit(ID, split="-")[[1]][2]
	return(ans)
}

sapply(ID, grab_num)



## ------------------------------------------------------
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="-")[[1]][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
#############################################################################

p.value<-vector(length=1000)

for(i in 1:1000){
	norm<-rnorm(30, mean=0, sd=1)
	group<-c(rep("control", 15), rep("treatment", 15))
	group<-sample(group)
	result<-t.test(norm[group=="control"], norm[group=="treatment"])
	p.value[i]<-result$p.value
}

####### 
seq<-sample(c("A", "C", "T", "G"),1000, replace=T)
table(seq)

 paste(c("A", "B", "C", "D"))
 for (i in ?){

	 x<-paste(seq[3:5], collapse="")
 

}






##### Not good 
control<-sample(norm, 15, replace=F)
treatment<-sample(norm, 15, replace=F)













   