R Programming - Lecture3
R Programming - Lecture3
> c(2,3,5,7) * 3
[1] 6 9 15 21
> mode(x)
[1] "numeric"
> x=8
>x
[1] 8
> mode(x)
[1] "numeric"
Variables
Varibale Names – Rules
• Allowed characters are Alphanumeric, ‘_’ and ‘.’
• Always start with alphabets
• No special characters like !,@,#,$,….
Examples:
Correct naming: > b2 =7
> Manoj_GDPL = “Scientist”
> Manoj.GDPL = “Scientist”
Wrong naming :
> 2b = 7
Error: unexpected input in “2b "
Predefined constants
Constant Symbol in R
Pi pi
letters a,b,c,…..x,y,z
LETTERS A,B,C,…X,Y,Z
certain datatype
Verify if object is of a certain Syntax: is.data_type(object) Example :
datatype use prefix “is.” before datatype as is.integer()
command.
Coerce or convert data type of use prefix “as.” before datatype as Syntax: as.data_type(object)
object to another command. Example :as.logical()
Note : Not all coercions are possible and if attempted will return “NA” as output
Example
Basic Objects
Object Values
> x2
[1] 1 4 9 16
ATTENTION: R is case sensitive (X is not the same as x)
Functions
Functions are a bunch of commands grouped together in a
sensible unit
Syntax
> abc(2,3)
[1] 13
> abc(3,4)
[1] 25
> abc(-2,-1)
[1] 5
Functions
Another example
> abc <- function(x){
sin(x)^2+cos(x)^2 + x
}
> abc(8)
[1] 9
> abc(899)
[1] 900
> abc(-2)
[1] -1
Matrix
Matrices are important objects in any calculation.
A matrix is a rectangular array with p rows and n columns.
An element in the i-th row and j-th column is denoted by Xij (book
version) or X[i, j] ("program version"), i = 1,2,...,n, j = 1,2,...,p.