-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_operations_class1.R
More file actions
64 lines (47 loc) · 1.2 KB
/
basic_operations_class1.R
File metadata and controls
64 lines (47 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# Anything that is typed after the "#" symbol is considered a comment and will not be evaluated at the console
# Using R like a basic calculator
1 + 4
# So we have an operand, an operator, and another operand. The operands are 1 and 4, and the operator is '+'.
1 * 4 # multiplication
1 - 4 # subtraction
4 / 2 # division
2 ^ 4 # power
# Operator precedence
5 + 2 * 2
(5 + 2) * 2
# What is a variable?
a <- 5 + 2 * 2
a # a is the name of the variable
# "<-" is the assignment operator.
# One can also use the "=" sign instead. I use the one above.
# What you find on the right is called the "expression" and this expression is assigned to the letter a, which can also
# be called an "object"
b <- (5 + 2) * 2
b
a + b
a / b
a ** b
a - b
# creating a vector
x <- c(1, 2)
x
y <- c(11, 23)
y
#Generation of sequences
1:10
10:1
?seq
seq(1, 10)
length(seq(1, 10)) # gives the number of elements in the vector
# Let us now create a variable and store the sequence in it.
u <- seq(1, 10)
# Now let us take a look at the length of this vector.
length(u)
# Let us create a new vector
v <- seq(11, 20)
# Let us do some mathematical operations on these vectors
u + v
u - v
u * v
u / v
# operations are element-wise