Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
38 views

R Programming - Lecture3

R can be used as a calculator to perform basic mathematical operations like addition, subtraction, multiplication, division etc. Variables can be assigned values and objects like vectors and matrices can be created. Functions allow grouping of commands to perform repetitive calculations. Matrices are important data structures that are represented as rectangular arrays with rows and columns in R.

Uploaded by

Azuyi Xr
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

R Programming - Lecture3

R can be used as a calculator to perform basic mathematical operations like addition, subtraction, multiplication, division etc. Variables can be assigned values and objects like vectors and matrices can be created. Functions allow grouping of commands to perform repetitive calculations. Matrices are important data structures that are represented as rectangular arrays with rows and columns in R.

Uploaded by

Azuyi Xr
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

Basics of Calculations

Basics, Built in Functions, Assignments, Variables, Data Types, Functions


& Matrices
Basics and R as a Calculator
> Is the prompt sign in R
The assignment operators are the left arrow with dash <- and equal
sign =
> x <- 10 assigns the value 10 to x
> x = 10 assigns the value 10 to x
Initially only <- was available in R

> x = 20 assigns the value 20 to x


> y = x * 2 assigns the value 2*x to y
> z = x + y assigns the value x + y to z
Basics and R as a Calculator
Capital and small letters are different
> X <- 20 and > x <- 20 are different

The command c(1,2,3,4,5) combines the numbers 1,2,3,4 and 5 to a


vector
Basics and R as a Calculator
> 2+4 #command
[1] 6 #output
> 2*5 #command
[1] 10 #output
> 2-3 #command
[1] -1 #output
> 3/2 #command
[1] 1.5 #output
> 2*3-4+5/6 #command
[1] 2.833333 #output
> 2^3 #command
[1] 8 #output
> 2**3 #command
[1] 8 #output
> 2^0.5 #command
[1] 1.414214 #output
> 2**0.5 #command
[1] 1.414214 #output
> 2^-0.5 #command
[1] 0.7071068 #output
> c(2,3,6,9)^2 #command, Vector
[1] 4 9 36 81 #output
Multiplication & Division x * y , x/y
> c(2,3,5,7)^c(2,3) # !!ATTENTION! Observe the operation > c(2,3,5,7) * 3
[1] 4 27 25 343 # output [1] 6 9 15 21

> c(1,2,3,4,5,6)^c(2,3,4) # command: application


to a vector with vector
[1] 1 8 81 16 125 1296 # output
Multiplication & Division x * y , x/y
> c(2,3,5,7)^c(2,3,4) #error message
[1] 4 27 625 49 # output

> c(2,3,5,7) * 3
[1] 6 9 15 21

> c(2,3,5,7) * c(-2,-3,-5,8)


[1] -4 -9 -25 56
Addition & Subtraction x+y, x-y
> c(2,3,5,7) + 10
[1] 12 13 15 17

> c(2,3,5,7) + c(-2,-3, -5, 8)


[1] 0 0 0 15
Addition & Subtraction x+y, x-y
> c(2,3,5,7) + c(8,9) # !!! ATTENTION!
[1] 10 12 13 16

> c(2,3,5,7) + c(8,9,10) # error message


[1] 10 12 15 15
Integer Division %/%
Integer Division: Division in which the fractional part (remainder) is discarded
> c(2,3,5,7) %/% 2
[1] 1 1 2 3

> c(2,3,5,7) %/% c(2,3)


[1] 1 1 2 2
Modulo Division (x mod y) %%
x mod y : modulo operation finds the remainder after division of one number by another
> c(2,3,5,7) %% 2
[1] 0 1 1 1

> c(2,3,5,7) %% c(2,3)


[1] 0 0 1 1
Maximum and Minimum: max and min
> max(1.2, 3.4, -7.8)
[1] 3.4

> min(1.2, 3.4, -7.8)


[1] -7.8
Some useful built in functions

abs() Absolute value

sqrt() Square root

round(), floor(), ceiling() Rounding, up and down

sum(), prod() Sum and product

log(), log10(), log2() Logarithms

exp() Exponential function

sin(), cos(), tan(), asin(), acos(), atan() Trigonometric functions

sinh(), cosh(), tanh(), asinh(), acosh(), Hyperbolic functions


atanh()
Some useful built in functions
Example:
> abs(-4)
[1] 4
> abs(c(-1,-2,-3,4,5))
[1] 1 2 3 4 5
> sqrt(4)
[1] 2
> sqrt(c(4,9,16,25))
[1] 2 3 4 5
Some useful in functions
> sum(c(2,3,5,7))
[1] 17
> prod(c(2,3,5,7))
[1] 210
> round(1.23)
[1] 1
> round(1.83)
[1] 2
Assignments
Assignments can be made in two ways:
> x<-6
>x
[1] 6

> 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

Months in a year month.name, month.abb


Data Types
Basic Data Types Values

Logical TRUE and FALSE

Integer Set of all Integers

Numeric Set of all real numbers

Complex Set of complex numbers

Character “a”,”b”,”c”,….,”x”,”y”,”z”,”@”,”#”,”$”, “”,”*”, “1”,”2”,… etc..


Data Types
TASK ACTION SYNTAX/EXAMPLE

Find data type of object use command “typeof()” Syntax: typeof(object)

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

Vector Ordered collection of same data types

List Ordered collection of objects

Data frame Generic tabular object


Assignments
An assignment can also be used to save values in variables:

> x1 <- c(1,2,3,4)

> x2 <- x1^2

> 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

Functions take input arguments, do calculations (or make some


graphics, call other functions) and produce some output and
return a result in a variable. The returned variable can be a
complex construct, like a list

Syntax

Name <- function(Argument1, Argument2, ...)


{
expression
}
where expression is a single command or a group of commands
Functions
Function arguments with description and default values
• Function arguments can be given a meaningful name
• Function arguments can be set to default values
• Functions can have the special argument ’...’
Functions (Single variable)
The sign <− is furthermore used for defining functions:
> abc <- function(x){
+x ^2
+}
> abc(3)
[1] 9
> abc(6)
[1] 36
> abc(-2)
[1] 4
Functions
Functions (Two Variables)
> abc <- function(x,y){
x^2+y^2
}

> 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.

An element of a matrix can also be an object, for example a string.


However, in mathematics, we are mostly interested in numerical
matrices, whose elements are generally real numbers
Matrix
In R, a 4 × 2-matrix X can be created with a following command:

> x <- matrix( nrow=4, ncol=2,


data=c(1,2,3,4,5,6,7,8) )
>x
[,1] [,2]
[1,] 1 5
[2,] 2 6
[3,] 3 7
[4,] 4 8
Matrix
We see:
The parameter nrow defines the row number of a matrix.
The parameter ncol defines the column number of a matrix.
The parameter data assigns specified values to the matrix
elements.
The values from the parameters are written column-wise in matrix.
>x
[,1] [,2]
[1,] 1 5
[2,] 2 6
[3,] 3 7
[4,] 4 8
One can access a single element of a matrix with x[i,j]:
> x[3,2]
[1] 7
Matrix
In case, the data has to be entered row wise, then a 4 × 2-matrix X
can be created with

> x <- matrix( nrow=4, ncol=2, data=c(1,2,3,4,5,6,7,8), byrow = TRUE)


>x
[,1] [,2]
[1,] 1 2
[2,] 3 4
[3,] 5 6
[4,] 7 8
Matrix

You might also like