0% found this document useful (0 votes)
18 views

RCourse-Lecture11-Calculations-Built in Functions and Assignments - Watermark

This document discusses built-in functions in R for calculations like max, min, mean, abs, sqrt, round, log, and assignments. It provides examples of using these functions to find the maximum, minimum, mean, absolute value, square root, rounding, and logs of values. It also shows how to save values in variables through assignments and do calculations with scalars, vectors, and functions.

Uploaded by

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

RCourse-Lecture11-Calculations-Built in Functions and Assignments - Watermark

This document discusses built-in functions in R for calculations like max, min, mean, abs, sqrt, round, log, and assignments. It provides examples of using these functions to find the maximum, minimum, mean, absolute value, square root, rounding, and logs of values. It also shows how to save values in variables through assignments and do calculations with scalars, vectors, and functions.

Uploaded by

kavithanjali
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Foundations of R Software

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.

R has built in functions for computations.

2
Maximum
Operator: max

> max(1.2, 3.4, -7.8)


[1] 3.4

> max( c(1.2, 3.4, -7.8) )


[1] 3.4

See the difference in use of c command.

3
Minimum
Operator: min

> min(1.2, 3.4, -7.8)


[1] -7.8

> min( c(1.2, 3.4, -7.8) )


[1] -7.8

See the difference in use of c command.

4
Arithmetic mean
Operator: mean

𝟐 𝟑 𝟒
> mean(2, 3, 4) 𝐦𝐞𝐚𝐧 𝟑
𝟑

[1] 2

> mean(c(2, 3, 4))


[1] 3

See the difference in use of c command.

Suggestion: Always use c command to input more than one data


value.

5
Overview Over Further 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(), Trigonometric functions


asin(), acos(), atan()
sinh(), cosh(), tanh(), Hyperbolic functions
asinh(), acosh(), atanh()
6
Examples
Operator: abs for finding the absolute values

> 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

Operator: prod for finding the product of values

> 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

> log(c(10, 100, 1000))


[1] 2.302585 4.605170 6.907755

11
Examples
Operator: log10 for finding the log (with base 10) of values

> log10(10)
[1] 1

> log10(100)
[1] 2

> log10(c(10, 100, 1000))


[1] 1 2 3

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.

> c(1,2,3,4) + sum(c(1,2,3,4)) * prod(c(1,2))


[1] 21 22 23 24

> abs( c(1,2,3,4) - sum(c(1,2,3,4))*prod(c(1,2)) )


[1] 19 18 17 16

14

You might also like