#clear the environment 
rm(list=ls()) 

#Check the working directory before importing else provide full path
#setwd(path)
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
## ------------------------------------------------------------------------
x=10 #here x is a double as 10 is a numeric value
#x<-10 is the same as x=10
typeof(x)#to check the type of data

## ------------------------------------------------------------------------
x=8.5
is.double(x) #to check if the data type is double

## ------------------------------------------------------------------------
x=9
typeof(x)

## ------------------------------------------------------------------------
x=as.integer(9)
typeof(x)

## ------------------------------------------------------------------------
#double
x=-1
#complex
y=-1+0i
sqrt(x) #results in an error
sqrt(y) 

## ------------------------------------------------------------------------
x=11
y=10
a=x>y
a
typeof(a)

## ------------------------------------------------------------------------
x="This is a string"
print(x)
x="a"
typeof(x)

## ------------------------------------------------------------------------
b.type=c("A","AB","B","O") #character object
#use factor function to convert to factor object
b.type=factor(b.type)
b.type
#to get individual elements (levels) in factor object
levels(b.type)

## ------------------------------------------------------------------------
date1="31-01-2012"
date1=as.Date(date1,"%d-%m-%Y")
date1

data.class(date1)
#The date and time are internally interpreted as Double so the function typeof will return the type Double
typeof(date1)

## ------------------------------------------------------------------------
date1=as.POSIXct("2012-01-01")
datetime1=as.POSIXct("2012-01-01 10:10")
date1
datetime1
#args can be used to see the arguments in a function for example
args(as.POSIXct)

## ------------------------------------------------------------------------
m.data=c("100","200","missing")
#convert m.data to double will create one missing value as "missing" is not a double
m.data=as.double(m.data)
#the warning message tells that an NA was insterted for a value which couldnt be converted to type double
is.na(m.data)#check for the missing value

## ------------------------------------------------------------------------
vec1=c(1,2,3,4,5)
vec1

## ------------------------------------------------------------------------
m1=matrix(c(1,2,3,4,5,6),nrow=3,ncol=2,byrow=TRUE)
#nrow-specify number of rows,
#ncol-specify number of columns,
#byrow-fill the matrix in rows with the data supplied
m1 #print the matrix

## ------------------------------------------------------------------------
m2=c(1,2,3,4,5,6)
dim(m2)=c(3,2)#the matrix will be filled by columns
m2
#use dim to get the dimension (#rows and #columns) of a matrix
dim(m1) 

## ------------------------------------------------------------------------
m3=m1*2 # all elements will be multiplied by 2 individually
m3

## ------------------------------------------------------------------------
dim(m1)# 3 rows and 2 columns
#create another matrix with 2 rows and 3 columns
#Note the use of  operator to create a sequence
m3=matrix(c(1:6),ncol=3)
m4=m1%*%m3

## ------------------------------------------------------------------------
z=c(1:24)#vector of length 24
#constructing a 3 by 4 by 2 array
a1=array(z,dim=c(3,4,2)) 
a1


## ------------------------------------------------------------------------
#element in the row 1 and column 3 in the first subset
a1[1,4,1]

## ----echo=-1-------------------------------------------------------------
options(str=list(vec.len=2))
#swiss dataframe has standardized fertility measure and socio-economic indicators for each of 47 French-speaking provinces of Switzerland at about 1888.
data(swiss)
str(swiss)

## ------------------------------------------------------------------------
#using names and row.names
names(swiss)#name of the columns (can also use colnames)
colnames(swiss)
row.names(swiss)#name of the rows
swiss$Fertility #returns the vector of data in the column Fertility


## ------------------------------------------------------------------------
num1=seq(1:5)
ch1=c("A","B","C","D","E")
df1=data.frame(ch1,num1)
df1

## ------------------------------------------------------------------------
e1 = c(2, 3, 5) #element-1
e2 = c("aa", "bb", "cc", "dd", "ee")  #element-2
e3 = c(TRUE, FALSE, TRUE, FALSE, FALSE)#element-3
e4=df1 #element-4 (previously constructed data frame)
lst1 = list(e1,e2,e3, e4)   # lst contains copies of e1,e2,e3,e4
str(lst1)#show the structure of lst1

## ------------------------------------------------------------------------
#first element of lst1
lst1[[1]]
lst1[1]


## ------------------------------------------------------------------------
names(lst1)=c("e1","e2","e3","e4")
names(lst1)#name of the elements
lst1$e1 #using $operator to refer the element
