Stats with R Programming FYCS
PRACT 3: Essential Data Structures in R:
1. Vectors
Vectors are a fundamental data structure in R. They are created using the c() function.
Q.1 Create a vector in R containing the numbers 1, 2, 3, 4, and 5. Perform the following
tasks:
1. Display the element at index 3.
2. Add 2 to each element of the vector and display the updated vector.
3. Find and print the length of the vector.
CODE:
# Creating a vector
vector <- c(1, 2, 3, 4, 5)
# Accessing elements
print(paste("Element at index 3:", vector[3]))
# Adding a value to each element
vector_plus_two <- vector + 2
print("Vector after adding 2:")
print(vector_plus_two)
# Vector length
print(paste("Length of the vector:", length(vector)))
Output
[1] "Element at index 3: 3"
[1] "Vector after adding 2:"
[1] 3 4 5 6 7
[1] "Length of the vector: 5"
R.K. Talreja College Asst. Prof. Shreya Tiwari
Stats with R Programming FYCS
2. Matrices
Matrices in R are created using the matrix() function. They are two-dimensional.
Q.2 Create a 3x3 matrix in R with elements from 1 to 9 filled by columns. Perform the
following tasks:
1. Access and print the element at the second row and third column.
2. Add 1 to each element in the matrix and display the updated matrix.
CODE:
# Creating a matrix
matrix <- matrix(1:9, nrow=3, byrow=TRUE)
# Accessing an element
print(paste("Element at row 2, column 3:", matrix[2, 3]))
# Adding 1 to each element
matrix_plus_one <- matrix + 1
print("Matrix after adding 1:")
print(matrix_plus_one)
Output
[1] "Element at row 2, column 3: 6"
[1] "Matrix after adding 1:"
[,1] [,2] [,3]
[1,] 2 3 4
[2,] 5 6 7
[3,] 8 9 10
R.K. Talreja College Asst. Prof. Shreya Tiwari
Stats with R Programming FYCS
3. Arrays
Arrays can have more than two dimensions and are created using the array() function.
Q.3 Create a 2x3x2 array in R with elements from 1 to 12. Perform the following tasks:
1. Access and print the element at position (1, 2, 2).
2. Add 10 to each element in the array and display the updated array
CODE:
# Creating a 3-dimensional array
array <- array(1:12, dim = c(2, 3, 2))
# Accessing an element
print(paste("Element at position (1, 2, 2):", array[1, 2, 2]))
# Adding 10 to each element
array_plus_ten <- array + 10
print("Array after adding 10:")
print(array_plus_ten)
Output
[1] "Element at position (2, 3, 1): 8"
[1] "Array after adding 10:"
[,1] [,2] [,3]
[1,] 11 12 13
[2,] 14 15 16
[,1] [,2] [,3]
[1,] 17 18 19
[2,] 20 21 22
R.K. Talreja College Asst. Prof. Shreya Tiwari
Stats with R Programming FYCS
4. Lists
Lists in R can contain elements of different types and are created using the list()
function.
Q.4) Create a list in R containing the following elements: a numeric vector with values 1, 2,
3, a character string "Data", and a logical value TRUE. Perform the following tasks:
1. Access and print the character string.
2. Add a new element to the list with the name "NewElement" and the value 100.
3. Remove the numeric vector from the list and print the updated list.
CODE:
# Creating a list
my_list <- list(numbers = c(1, 2, 3), text = "Data", flag = TRUE)
# Accessing the character string
print(paste("Text element:", my_list$text))
# Adding a new element
my_list$NewElement <- 100
print("List after adding 'NewElement':")
print(my_list)
# Removing the numeric vector
my_list$numbers <- NULL
print("List after removing 'numbers':")
print(my_list)
Output
[1] "Text element: Data"
[1] "List after adding 'NewElement':"
$numbers
[1] 1 2 3
R.K. Talreja College Asst. Prof. Shreya Tiwari
Stats with R Programming FYCS
$text
[1] "Data"
$flag
[1] TRUE
$NewElement
[1] 100
[1] "List after removing 'numbers':"
$text
[1] "Data"
$flag
[1] TRUE
$NewElement
[1] 100
5. Data Frames
Q.5) Create a data frame in R with the following columns: StudentName (containing "Alice",
"Bob", "Charlie"),Age (containing 25, 30, 35), and City (containing "New York", "Los
Angeles", "Chicago"). Perform the following tasks:
1. Display the entire data frame.
2. Access and print the Age column.
3. Add a new column Occupation with certain values for all rows and print the updated
data frame.
Data frames are used to store tabular data and are created using the data.frame()
function.
CODE:
# Creating a data frame
df <- data.frame(
Name = c("Alice", "Bob", "Charlie"),
R.K. Talreja College Asst. Prof. Shreya Tiwari
Stats with R Programming FYCS
Age = c(25, 30, 35),
City = c("New York", "Los Angeles", "Chicago")
# Displaying the data frame
print("Data Frame:")
print(df)
# Accessing a column
print("Ages of people:")
print(df$Age)
# Adding a new column
df$Occupation <- c("Engineer", "Artist", "Teacher")
print("Data Frame with new column:")
print(df)
Output
[1] "Data Frame:"
Name Age City
1 Alice 25 New York
2 Bob 30 Los Angeles
3 Charlie 35 Chicago
[1] "Ages of people:"
[1] 25 30 35
[1] "Data Frame with new column:"
Name Age City Occupation
1 Alice 25 New York Engineer
2 Bob 30 Los Angeles Artist
R.K. Talreja College Asst. Prof. Shreya Tiwari
Stats with R Programming FYCS
3 Charlie 35 Chicago Teacher
6. Functions
Functions in R are created using the function keyword. They help encapsulate code
into reusable blocks
Q.6) Write two functions in R:
1. multiply_numbers(x, y) that returns the product of x and y.
2. is_even(number) that returns TRUE if the number is even and FALSE otherwise.
Perform the following tasks:
1. Use multiply_numbers to find the product of 4 and 7 and print the result.
2. Use is_even to check if the number 10 is even and print the result.
CODE:
# Defining the function to multiply two numbers
multiply_numbers <- function(x, y) {
return(x * y)
# Defining the function to check if a number is even
is_even <- function(number) {
return(number %% 2 == 0)
# Using 'multiply_numbers' to find the product of 4 and 7
product <- multiply_numbers(4, 7)
print(paste("Product of 4 and 7:", product))
# Using 'is_even' to check if 10 is even
even_check <- is_even(10)
print(paste("Is 10 even?", even_check))
Output:
[1] "Product of 4 and 7: 28"
[1] "Is 10 even? TRUE"
R.K. Talreja College Asst. Prof. Shreya Tiwari