CT127-3-2 Programming for Data Analysis Basics of R
Tutorial 2- Basics of R
1) Try the basic math commands by typing the following expressions in the script
5+17
10-7
4*3*5
18/9
9%%4
(5*8)+(12-7)
2) Try defining/assigning values/ removing variables:
i. Define a new variable, weeks, then assign 4 to it using the operator (<-).
ii. Define a new variable, hoursPerWeek, then assign 40 to it using the operator
(=).
iii. Define a new variable, hourlyRate, then assign 50 to it using assign function.
i. Define a new variable, x, then assign 100 to it using any assignment operator.
Then, remove it using rm function.
3) Define new variables with different data types. Then, check the data type using
class(), [Link](), [Link] (), [Link](), and
[Link]() functions.
4) Define a vector, test_Marks, which contains the marks for 5 students in a test.
Then, define another vector, final_Marks, to store the marks for the 5 students in
the final exam. Test marks must be any value in the range 0-30, while final exam marks
must be any value in the range 0-70. Calculate and print the total marks for each student
(total_Marks).
5) Define a new vector, pass, that contains a logical value TRUE if the student’s total
mark, which was calculated in Exercise 4, is greater than or equal to 50.
6) Find the minimum, maximum, mean, and median values of total_Marks vector
using min, max, mean, and median functions, respectively.
Level-2 Asia Pacific University of Technology and Innovation Page 1 of 2
CT127-3-2 Programming for Data Analysis Basics of R
Take home exercises:
1) Give names to each final mark in total_Marks to represent students’ names by using
names function.
2) Try applying these functions:
round(547.8)
length(total_Marks)
sqrt(9)
substr(“abcd”, 2,4)
strsplit("hello world", " ")
paste("welcome to”, “PFDA")
nchar(“hello”)
Level-2 Asia Pacific University of Technology and Innovation Page 2 of 2