NOTE 4: BASIC OPERATIONS IN R
1
Operators in R
2
Built-in Functions in R
3
Built-in Functions in R Cont…
4
BASIC OPERATIONS IN R
• Two or more expressions can be on a single line so long as they separated by a
semi-colon:
>1+1; 2*9; 10/4
• Any R code after the symbol “#” is considered by R as a comment. In fact, R
does not interpret it.
>log(100) #logarithm of 100
>sqrt(2) #square root of 2
5
Modulo and Integer Quotient
Modulo: the modulo operation returns the remainder of a division, after one number
is divided by another.
Ex: 50 𝑚𝑜𝑑 8 = 2 2 is left when 50 is divided by 8
>50%%8
Integer quotient: is the integer part of a division
Ex: when 50 is divided by 8, the integer quotient is 6
>50%/%8
6
Creating Data
• The function ":" generates a sequence of integers.
>1:10 #integers from 1 to 10
[1] 1 2 3 4 5 6 7 8 9 10
• The function seq() generates a sequence of values as a vector.
>seq(from=4,to=5) #integers from 4 to 5
[1] 4 5
>seq(from=4,to=5,by=0.1) #numbers from 4 to 5 with a gap of 0.1
[1] 4.0 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9 5.0
>seq(from=4,to=5,length=8) #numbers from 4 to 5 such that the length of the vector is 8
[1] 4.000000 4.142857 4.285714 4.428571 4.571429 4.714286 4.857143 5.000000
7
Creating Data Cont…
• The function rep() replicates the values of its first argument in several smart ways.
>rep(1,4)
[1] 1 1 1 1
>rep(1:4, 2)
[1] 1 2 3 4 1 2 3 4
>rep(1:4, each = 2)
[1] 1 1 2 2 3 3 4 4
>rep(1:4, c(2,1,2,3))
[1] 1 1 2 3 3 4 4 4
>rep(1:4, each = 2, len = 4)
[1] 1 1 2 2
>rep(1:4, each = 2, len = 10)
[1] 1 1 2 2 3 3 4 4 1 1
>rep(1:4, each = 2, times = 3)
[1] 1 1 2 2 3 3 4 4 1 1 2 2 3 3 4 4 1 1 2 2 3 3 4 4
8
R as a Sampler
• Suppose we have 25 people and we want to select 5 of them randomly. Who
would we select?
>sample(25, 5)
[1] 20 23 19 5 9
>[Link](100)
>sample(25, 5)
[1] 10 23 6 16 19
> sample(25, 5, replace = TRUE)
[1] 6 4 23 6 2
> sample(25, 5, replace = FALSE)
[1] 7 25 23 11 18
9
Accessing Data
There are several ways to extract data from a vector.
Suppose x is a numerical vector with numbers from 1 to 10
• To find how many elements (length)
>length(x)
• To print ith element (i = 2)
>x[2]
• To print all the elements, except the ith element (i = 2)
>x[-2]
10
Accessing Data Cont…
• To print all but not specific elements (all the elements of x except 1st three
elements)
>x[-c(1:3)]
• To print first k elements (k = 4)
>head(x, 4)
>x[1:4]
• To print last k elements (k = 4)
>tail(x, 4)
>x[(length(x)-3):length(x)]
• To print specific elements (1st, 3rd and 5th)
>x[c(1,3,5)]
11
Accessing Data Cont…
• To print the values of a vector which are greater than some value (the value is 3)
>x[x>3]
• To print the values which are bigger than or less than some values
>x[x<2| x>6]
• To print the index of the maximum value of the vector
>which(x==max(x))
• To print the index of the minimum value of the vector
>which(x==min(x))
12
Sorting, Ordering and Ranking
Suppose y is a numerical vector with the elements 1,3,6,2,7,4,8,1,0
• sort(): sorts the elements of a vector, in increasing or decreasing order.
>sort(y)
[1] 0 1 1 2 3 4 6 7 8
>sort(y, decreasing = TRUE)
[1] 8 7 6 4 3 2 1 1 0
• order() : returns the vector of (increasing or decreasing) ranking indices of the elements.
>order(y)
[1] 9 1 8 4 2 6 3 5 7
• rank() : returns the vector of ranks of the elements.
>rank(y)
[1] 2.5 5.0 7.0 4.0 8.0 6.0 9.0 2.5 1.0
13
Exercise
Create a new project “Exercise” and save all your work inside this project.
The codes should be provided in a R-script file named “Exe”.
Give question numbers as comments.
1. Calculate 10 times 3
2. Define a variable x equals to 5 and find x times 10.
3. Define a vector y with numbers from 1 to 5. Add 3 to each element and save it in a new
vector called z. Print y and z.
4. Define a vector X, which is a sample of 12 numbers between 3 and 30. Test whether
each of the elements in the vector is greater than 10 or not. Return TRUE or FALSE.
5. Create a sequence of numbers from 20 to 50 and find the minimum, maximum, mean,
median, sum, standard deviation, variance and length of the sequence.
6. Find 15!
14
Exercise Cont…
7. Round off 152.2389413862 to its nearest 1st, 2nd and 7th decimal point.
8. Find the integer quotient and modulo of 1734 divided by 6
9. Let A be a vector with numbers 21, 49, 160, 72, 86, 356, 55, 67, 91 and 30.
i. Print the length of the vector
ii. Print the 6th element of the vector
iii. Print first 3 elements and the last 3 elements
iv. Print all the elements greater than 100
v. Sort the vector in ascending and descending order.
10. Create the vector B = 5, 10, 15, 20, 25, 30, 35, 40, 45, 50 using seq() function.
11. Create the C vector which contains 10 random integer values between -50 and +50.
12. Check which elements of B are not equal to C.
15