RCourse Lecture4 Calculations
RCourse Lecture4 Calculations
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 =.
3
Basics
Capital and small letters 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
6
R as a calculator
> 2-3 # Command
[1] -1 # Output
7
R as a calculator
> 2^3 # Command
[1] 8 # Output
8
R as a calculator
> 2^0.5 # Command
[1] 1.414214 # Output
9
R as a calculator
2 2 2 2
2, 3, 5, 7
10
R as a calculator
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
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
18
Addition and Subtraction x + y, x y
> c(2,3,5,7) + c(-2,-3, -5, 8)
[1] 0 0 0 15
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