Lab 1 – R Basics & Vectors
Answer the following questions by writing and running R code. Each question is designed to help you practice key
R concepts, from basic syntax to vector operations.
1) Getting Started with R
Task 1.1 – Write R code to print the message “Hello, Data Science!” to the console.
Task 1.2 – Write R code to display your current working directory, and then list all files in that directory.
2) Arithmetic Operations
Task 2.1 – Using R, calculate the results of the following operations:
1. 8 + 12
2. 15 - 9
3. 4 × 7
4. 20 / 5
5. 2 raised to the power of 5
Task 2.2 – In R, compute the value of the expression: (15 + 3) × 4 - 2^3.
3) Variables and Assignment
Task 3.1 – Create two variables: a with value 25, and b with value 4. Using these variables, calculate their sum,
product, and the result of dividing a by b.
Task 3.2 – Change the value of a to 50, and then recalculate the sum of a and b.
4) Basic Data Types
Task 4.1 – Create four variables in R: an integer, a decimal (numeric), a character string, and a logical value
(TRUE or FALSE). Use the class() function to check the type of each variable.
Task 4.2 – Convert a numeric variable to a character variable and verify its type using the class() function.
5) Creating Vectors
Task 5.1 – Create a numeric vector containing the integers from 1 to 10, a character vector with the names of
three cities, and a logical vector containing at least one TRUE and one FALSE value.
Task 5.2 – Use the length() function to determine the size of each vector you created.
6) Vector Operations
Given the following vectors:
x <- c(2, 4, 6, 8)
y <- c(1, 3, 5, 7)
Task 6.1 – Perform the following operations: add x and y, subtract y from x, multiply x and y element-wise, and
divide x by y.
Task 6.2 – Multiply every element in vector x by 2.
7) Comparison Operators
Using the vector x <- c(2, 4, 6, 8):
1. Identify which elements are greater than 4.
2. Identify which elements are equal to 6.
3. Identify which elements are less than or equal to 5.
8) Vector Indexing & Slicing
From the vector x <- c(2, 4, 6, 8):
1. Extract the second element.
2. Extract the first three elements.
3. Remove the last element from the vector.
4. Select only the values greater than 5.
9) Mini Challenge (Advanced)
You are given the vector:
hours <- c(5, 8, 2, 10, 7, 4, 12, 9)
1. Extract the study hours of students who studied more than the average hours.
2. From the extracted values, keep only those that are less than 10.
3. Increase each remaining value by 1 hour.
4. Calculate the mean, minimum, and maximum of the updated vector.
5. Sort the updated vector in descending order and display the top two values.
10) Mini Challenge 2 – Student Exam Analysis
You are given the following data:
names <- c("Anna", "Ben", "Chloe", "David", "Eva", "Frank")
scores <- c(78, 92, 56, 88, 45, 67)
1. Display the names of students who scored more than 80.
2. Display the names of students who failed the exam (score less than 50).
3. Add 5 bonus points to all scores that are below the average score.
4. Find the new average score and the name of the student with the highest score.
5. Display the names of students whose scores are at least 60 but less than 90 after adding the bonus points.