RCourse-Lecture11-Calculations-Built in Functions and Assignments - Watermark
RCourse-Lecture11-Calculations-Built in Functions and Assignments - Watermark
Lecture 11
Basics of Calculations
::::
Built in Functions and Assignments
Shalabh
Department of Mathematics and Statistics
Indian Institute of Technology Kanpur
1
R as a calculator
R can perform all standard calculations like addition, subtraction,
multiplication, division etc.
2
Maximum
Operator: max
3
Minimum
Operator: min
4
Arithmetic mean
Operator: mean
𝟐 𝟑 𝟒
> mean(2, 3, 4) 𝐦𝐞𝐚𝐧 𝟑
𝟑
[1] 2
5
Overview Over Further Functions
abs() Absolute value
> abs(-4)
[1] 4
> abs(c(-1,-2,-3,4,5))
[1] 1 2 3 4 5
7
Examples
Operator: sqrt for finding the square root of values
> sqrt(4)
[1] 2
> sqrt(c(4,9,16,25))
[1] 2 3 4 5
8
Examples
Operator: sum for finding the sum of values
> sum(c(2,3,5,7))
[1] 17
> prod(c(2,3,5,7))
[1] 210
9
Examples
Operator: round for finding the round off values
> round(1.23)
[1] 1
> round(1.83)
[1] 2
10
Examples
Operator: log for finding the natural log (ln) of values
> log(10)
[1] 2.302585
> log(exp(1))
[1] 1
11
Examples
Operator: log10 for finding the log (with base 10) of values
> log10(10)
[1] 1
> log10(100)
[1] 2
12
Assignments
An assignment can also be used to save values in variables:
> x1 = c(1,2,3,4)
> x1
[1] 1 2 3 4
> x2 = x1^2
> x2
[1] 1 4 9 16
13
Assignments
Calculations with scalar, data vector and built in functions also work.
14