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

RCourse Lecture5 Calculations

This document provides an introduction to basic calculations and functions in R software. It discusses integer and modulo division, maximum and minimum values, and common mathematical functions like absolute value, square root, rounding, summation, and logarithms. It also demonstrates how to perform assignments by saving values in variables to reuse calculations. The key functions and operations introduced are %/% for integer division, %% for modulo division, max() and min() for extrema, and assignments using <- and = to save values to variables.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

RCourse Lecture5 Calculations

This document provides an introduction to basic calculations and functions in R software. It discusses integer and modulo division, maximum and minimum values, and common mathematical functions like absolute value, square root, rounding, summation, and logarithms. It also demonstrates how to perform assignments by saving values in variables to reuse calculations. The key functions and operations introduced are %/% for integer division, %% for modulo division, max() and min() for extrema, and assignments using <- and = to save values to variables.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Introduction to R Software

Basics of Calculations
::::
R as Calculator, Built in Functions and Assignments

Shalabh
Department of Mathematics and Statistics
Indian Institute of Technology Kanpur

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

2% / %2, 3% / %2, 5% / %2, 7% / %2

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

2% / %2, 3% / %3, 5% / %2, 7% / %3

3
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

2%%2, 3%%2, 5%%2, 7%%2

4
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) %% c(2,3)
[1] 0 0 1 1

2%%2, 3%%3, 5%%2, 7%%3

5
Maximum: max
> max(1.2, 3.4, -7.8)
[1] 3.4

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


[1] 3.4

6
Minimum : min
> min(1.2, 3.4, -7.8)
[1] -7.8

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


[1] -7.8

7
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()
8
Examples
> abs(-4)
[1] 4

> abs(c(-1,-2,-3,4,5))
[1] 1 2 3 4 5

9
Examples
> sqrt(4)
[1] 2

> sqrt(c(4,9,16,25))
[1] 2 3 4 5

10
Examples
> 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

11
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" 12
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) 13

You might also like