Tuesday, 7 February 2017

R - Basics









#Basic Calculations
#Removing if any ojects already exists in this current environment
#rm function is used to remove the ojects from the workspace
rm(list = ls())

#----------------------------
#Adding numbers
#----------------------------
1+3
#Adding numbers those are in Objects
Num1=10
Num2 = 15
Num3=Num1+Num2+5
Num3
#----------------------------
# Multiplications
#----------------------------
10*3
Num4= Num1*2
Num4
Num5= Num1*Num2
Num5
Num6= Num1*Num2*1.5
Num6
#----------------------------
#Reminder, As we known 10 devides 3 , remain is 1, syntax is a%%b
#----------------------------
remain<- 10%%3
remain
Rem1<- Num2%%Num1
#----------------------------
#division is used to know to division of Num1 divided by Num2, name is Modulos, syntax is a%/%b
#----------------------------
Div1 <- 10%/%3A
Div1
Div2 <- Num2%/%Num1
Div2
Div3 <- 100%/%30
Div3
Reminder3<- 100%%30
Reminder3
#----------------------------
#exponetials We read it as A to the Power of B , sytax is a^b ( shift+6 is the power symbol)
#----------------------------
10^2
10^3
a=10; b=3; c= 0.5;
Num_exp1<- (a^b)
Num_exp1
Num_exp2<- (a^b)^c
Num_exp2
#we can define how many decimals want to see in the output
Num_exp2
Num_exp2_3decimals <- round(Num_exp2, digits = 3)
Num_exp2_3decimals

#----------------------------
#Comparing 2 numbers, it yeilds result as boolean , it means TRUE or FALSE
#----------------------------
A1 = 100
A2=200
A3=300
A1>A2
A1<A2
#Comapring wherther 2 numners are equal ot not
A3== (A1+A2)





# Vector with 1 element, V1 vector holds data 1
V1<- 1
# Vector with more than 1 elements, V2_1 vector holds data 1,2,3,4,5,6
#using combination finction "c" (lower case c)
v2_1<- c(1,2,3,4,5,6)
#----- the below are
V2_2<- 1:6
V2_2
V2_3<- seq(from=1, to = 6, by=1)
V2_3
#Vector with string data
c1<- c('a','b', '', 'd','e',"F", 'g' )
#We can access wach element by using the index
#Accessing the 3rd element, i.e. no data for 3rd element
c1[3]
#assigning values, replacing 3rd element from null to c.
c1
c1[3]<-'c'
c1
#Replacing 6th element from F to f
c1[6]
c1[6]<- 'f'
c1[6]

# Similarly, we can do it for Number vector

#------------------------------------
#copying 1 object elements into another ojbect
O1 = c(1,2,3,4)
O2 = O1
O2





No comments:

Post a Comment