# This R environment comes with all of CRAN preinstalled, as well as many other helpful packages
# The environment is defined by the kaggle/rstats docker image: https://github.com/kaggle/docker-rstats
# For example, here's several helpful packages to load in 

library(ggplot2) # Data visualization
library(readr) # CSV file I/O, e.g. the read_csv function

# Input data files are available in the "../input/" directory.
# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory

#system("ls ../input")

# Any results you write to the current directory are saved as output.

#create and print a variable
mystring<-"Hello World!"
print(mystring)
print(class(mystring))

#create a vector
#using function c()
vec<-c('red','blue','black')
print(vec)

#create a matrix witch has tow dimentions
M<-matrix(c('a','b','c','d','e','f'),nrow=2,ncol=3,byrow=TRUE)
print(M)

#create an array
a <- array(c('green','yellow'),dim=c(3,3,2))
print(a)

BMI <- 	data.frame(
			gender = c("Male", "Male","Female"), 
			height = c(152, 171.5, 165), 
			weight = c(81,93, 78),
			Age =c(42,38,26)
			)
print(BMI)

# Assignment using equal operator.
var.1 = c(0,1,2,3)           

# Assignment using leftward operator.
var.2 <- c("learn","R")   

# Assignment using rightward operator.   
c(TRUE,1) -> var.3           

print(var.1)
cat ("var.1 is ", var.1 ,"\n")
cat ("var.2 is ", var.2 ,"\n")
cat ("var.3 is ", var.3 ,"\n")

#repeat
v <- c("Hello","loop")
cnt <- 2
repeat{
   print(v)
   cnt <- cnt+1
   if(cnt > 5){
      break
   }
}

#while
v <- c("Hello","while loop")
cnt <- 2
while (cnt < 7){
   print(v)
   cnt = cnt + 1
}

#for loop
v <- LETTERS[1:4]
for ( i in v) {
   print(i)
}

#next which is like continue
v <- LETTERS[1:6]
for ( i in v){
   if (i == "D"){
      next
   }
   print(i)
}

##
# Create a function to print squares of numbers in sequence.
new.function <- function(a) {
  for(i in 1:a) {
		b <- i^2
		print(b)
		}
}

new.function(6)

##
# Create a function with arguments.
new.function <- function(a,b,c) {
		result <- a*b+c
		print(result)
				 }

# Call the function by position of arguments.
new.function(5,3,11)

# Call the function by names of the arguments.
new.function(a=11,b=5,c=3)

##
# Create a function with arguments.
new.function <- function(a = 3,b =6) {
		result <- a*b
		print(result)
				     }

# Call the function without giving any argument.
new.function()

# Call the function with giving new values of the argument.
new.function(9,5)

##
# Create vector with elements from 5 to 9 incrementing by 0.4.
print(seq(5, 9, by=0.4))