#clear the environment 
rm(list=ls()) 
#setwd(path)
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
## ------------------------------------------------------------------------
## to be able to read the data you must have the csv file in the same directory with the prog.R file 
data_stocks=read.csv("us_stocks.csv")
#remove NAs from the data

data_stocks=na.omit(data_stocks)
m_msft=mean(data_stocks$MSFT) 
m_aapl=mean(data_stocks$AAPL)
if(m_msft>m_aapl){ 
g_mean=m_msft }else{
g_mean=m_aapl
}
g_mean #print greater mean

## ------------------------------------------------------------------------
 g_mean=if(m_msft>m_aapl)m_msft else m_aapl
g_mean

## ------------------------------------------------------------------------
#arguments to ifelse
args(ifelse)
g_mean=ifelse(m_msft>m_aapl,m_msft,m_aapl)
g_mean

## ------------------------------------------------------------------------
doit="mean" 
switch (doit, 
        mean={mean(data_stocks$MSFT)},
        median={median(data_stocks$MSFT)}
        ) 
#try the above with doit=median
doit="median"
switch (doit, 
        mean={mean(data_stocks$MSFT)},
        median={median(data_stocks$MSFT)}
        ) 

## ------------------------------------------------------------------------
#construct the loop
j=0
for(i in 1:15){
j=j+i #add i to j 
print(j)#print the sequential sum
}

## ------------------------------------------------------------------------
#intialise j and i
j=0
i=1
#one can also use i<=15
while (i<16){
j=j+i
i=i+1
print(j)
}

## ------------------------------------------------------------------------
#initialize
j=0
i=1
repeat {
j=j+i
i=i+1
print(j)
if(i>15) break
}

## ------------------------------------------------------------------------
vec1=as.vector(matrix(1,nrow=15))
for(i in 2:15){
  vec1[i]=vec1[i-1]+i #add i to j 
  }
#the following function takes 2 arguments, x a data frame, dates to indicate if there are dates in the first column

cal_mean=function(x,dates=TRUE){
num_cols=ncol(x)#calculate the number of columns
#lets use a list and a loop to refresh our concepts
m_stocks=list()#creating an empty list

#use for loop
#assign the starting value based on the dates column,we skip dates column if they are present (dates are basically row names to more generalised version will be to check for row names)
l=ifelse(dates==TRUE,2,1)
j=1#starting point in the list m_stocks
for (i in l:num_cols)
{
m_stocks[[j]]=mean(x[,i])
j=j+1
}
names(m_stocks)=colnames(x[,l:num_cols])
return(m_stocks)
}
#lets call the function cal_mean
cal_mean(data_stocks,TRUE)
#lets call the function with no dates column
cal_mean(data_stocks[,2:ncol(data_stocks)],FALSE)

