[go: up one dir, main page]

0% found this document useful (0 votes)
52 views21 pages

RCourse Lecture4 Calculations

This document provides an introduction to basic calculations in R software. It discusses using R as a calculator to perform arithmetic operations like addition, subtraction, multiplication, and division on vectors. Functions like c() are used to combine numbers into a vector. Operators like +, -, *, /, ^ can be applied to both scalars and vectors, but care must be taken when vectors have unequal lengths to avoid errors. Comments are denoted with # and ignore all text after it on a line.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views21 pages

RCourse Lecture4 Calculations

This document provides an introduction to basic calculations in R software. It discusses using R as a calculator to perform arithmetic operations like addition, subtraction, multiplication, and division on vectors. Functions like c() are used to combine numbers into a vector. Operators like +, -, *, /, ^ can be applied to both scalars and vectors, but care must be taken when vectors have unequal lengths to avoid errors. Comments are denoted with # and ignore all text after it on a line.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Introduction to R Software

Basics of Calculations
::::
Basics and R as a Calculator

Shalabh
Department of Mathematics and Statistics
Indian Institute of Technology Kanpur

1
Basics
> is the prompt sign in R.

The assignment operators are the left arrow with dash <- and equal sign =.

> x <- 20 assigns the value 20 to x.

> x = 20 assigns the value 20 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.


2
Basics
# : The character # marks the beginning of a comment. All characters until

the end of the line are ignored.

> # mu is the mean

> # x <- 20 is treated as comment only

3
Basics
Capital and small letters are different.

> X <- 20 and > x <- 20 are different

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

5
R as a calculator
> 2+3 # Command
[1] 5 # Output

> 2*3 # Command


[1] 6 # Output

6
R as a calculator
> 2-3 # Command
[1] -1 # Output

> 3/2 # Command


[1] 1.5 # Output

> 2*3-4+5/6 # Command


[1] 2.8333 # Output

7
R as a calculator
> 2^3 # Command
[1] 8 # Output

> 2**3 # Command


[1] 8 # Output

8
R as a calculator
> 2^0.5 # Command
[1] 1.414214 # Output

> 2**0.5 # Command


[1] 1.414214 # Output

> 2^-0.5 # Command


[1] 0.7071068 # Output

9
R as a calculator

> c(2,3,5,7)^2 # command: application to a


vector
[1] 4 9 25 49 # output

2 2 2 2
2, 3, 5, 7

10
R as a calculator

> c(2,3,5,7)^c(2,3) # !!ATTENTION! Observe the


operation
[1] 4 27 25 343 # output

22 , 33 , 52 , 73

11
R as a calculator
> 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

12 , 23 , 34 , 42 , 53 , 64

12
R as a calculator
> c(2,3,5,7)^c(2,3,4) #error message
[1] 4 27 625 49 # output
Warning message:
longer object length is not a multiple of
shorter object length in: c(2,3,5,7)^c(2,3,4)

22 , 33 , 54 , 72

13
Multiplication and Division x y, x/y:
> c(2,3,5,7) * 3
[1] 6 9 15 21

2 3, 3 3, 5 3, 7 3

14
Multiplication and Division x y, x/y:
> c(2,3,5,7) * c(-2,-3,-5,8)
[1] -4 -9 -25 56

2 (2), 3 (3), 5 (5), 7 8

15
Multiplication and Division x y, x/y:
> c(2,3,5,7) * c(8,9) # !!! ATTENTION
[1] 16 27 40 63

2 8, 3 9, 5 8, 7 9

16
Multiplication and Division x y, x/y:
> c(2,3,5,7) * c(8,9,10) # error message
[1] 16 27 50 56
Warning message:
longer object length
is not a multiple of shorter object length
in: c(2, 3, 5, 7) * c(8, 9, 10)

2 8, 3 9, 5 10, 7 8

17
Addition and Subtraction x + y, x y
> c(2,3,5,7) + 10
[1] 12 13 15 17

2 10, 3 10, 5 10, 7 10

18
Addition and Subtraction x + y, x y
> c(2,3,5,7) + c(-2,-3, -5, 8)
[1] 0 0 0 15

2 (2), 3 (3), 5 (5), 7 8

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

2 8, 3 9, 5 2, 7 9

20
Addition and Subtraction x + y, x y
> c(2,3,5,7) + c(8,9,10) # error message
[1] 10 12 15 15
Warning message:
longer object length
is not a multiple of shorter object length
in: c(2, 3, 5, 7) + c(8, 9, 10)

2 8, 3 9, 5 10, 7 8

21

You might also like