0% found this document useful (0 votes)
5 views

Big Data Lab R Code with output.docx

The document contains a series of R programming assignments that cover various tasks such as finding maximum and minimum values in a vector, sorting vectors, comparing data frames, extracting letters, calculating sum, mean, and product of vectors, creating bar plots, and manipulating data frames. Each assignment includes R code snippets and expected outputs. The document serves as a practical guide for learning R programming through hands-on exercises.

Uploaded by

shraddhavinod1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Big Data Lab R Code with output.docx

The document contains a series of R programming assignments that cover various tasks such as finding maximum and minimum values in a vector, sorting vectors, comparing data frames, extracting letters, calculating sum, mean, and product of vectors, creating bar plots, and manipulating data frames. Each assignment includes R code snippets and expected outputs. The document serves as a practical guide for learning R programming through hands-on exercises.

Uploaded by

shraddhavinod1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

ASIGNMENT 1

Q.1​ Write an R program to find the maximum and the minimum value of a given vector.

​ R Code:

# Define the vector my_vector <- c(12, 45, 3, 67, 23, 89, 1)
# Calculate the maximum value max_value <- max(my_vector)
# Calculate the minimum value min_value <- min(my_vector)
# Print the maximum value
cat("The maximum value is:", max_value, "\n")
# Print the minimum value
cat("The minimum value is:", min_value, "\n")

OUTPUT:

Q.2 ​ Write an R program to sort a Vector in ascending and descending order.


​ R Code:

# Define the vector


my_vector <- c(34, 12, 5, 67, 23, 89, 1)
# Sort the vector in ascending order
ascending_order <- sort(my_vector)
# Sort the vector in descending order
descending_order <- sort(my_vector, decreasing = TRUE)
# Print the vector in ascending order
cat("Vector in ascending order:", ascending_order, "\n")
# Print the vector in descending order
cat("Vector in descending order:", descending_order, "\n")
OUTPUT:

Q.3 ​ Write an R program to compare two data frames to find the elements in first data frame that
are not present in second data frame.# Create two sample data frames
​ R Code:

df1 <- data.frame(ID = c(1, 2, 3, 4),


Name = c("Alice", "Bob", "Charlie", "David"))
df2 <- data.frame(ID = c(2, 3, 5),
Name = c("Bob", "Charlie", "Eve"))
unique_df1 <- df1[!df1$ID %in% df2$ID, ]
cat("Elements in the first data frame that are not present in the second data frame:\n")
print(unique_df1)

OUTPUT:
ASSIGNMENT 2
Q.1​ Write an R program to extract first 10 English letter in lower case and last 10 letters in
upper case and extract letters between 22nd to 24th letters in upper case.

R Code:

alphabet <- letters # Lowercase letters


uppercase_alphabet <- LETTERS # Uppercase letters
first_10_lower <- alphabet[1:10]
last_10_upper <- uppercase_alphabet[17:26]
letters_22_to_24_upper <- uppercase_alphabet[22:24]
cat("First 10 lowercase letters:", first_10_lower, "\n")
cat("Last 10 uppercase letters:", last_10_upper, "\n")
cat("Letters from 22nd to 24th positions in uppercase:", letters_22_to_24_upper, "\n")

OUTPUT:
Q.2​ Write an R program to find Sum, Mean and Product of a Vector.

R Code:

my_vector <- c(1, 2, 3, 4, 5)


vec_sum <- sum(my_vector)
vec_mean <- mean(my_vector)
vec_product <- prod(my_vector)

print(paste("Sum: ", vec_sum))


print(paste("Mean: ", vec_mean))
print(paste("Product: ", vec_product))

​ OUTPUT:

Q.3 Write an R program to create a simple bar plot of five subject’s marks.

​ R Code:

# Define the subjects and their corresponding marks


subjects <- c("Math", "Science", "English", "History", "Art")
marks <- c(85, 90, 78, 88, 92)

# Create a bar plot


barplot(marks,
names.arg = subjects,
col = "skyblue",
main = "Marks of Five Subjects",
xlab = "Subjects",
ylab = "Marks",
ylim = c(0, 100))

OUTPUT:
ASSIGNMENT 3
Q.1 Write an R program to create a Dataframes which contain details of 5 employees and display
the details in ascending order.

R Code:

# Create a data frame with employee details


employee_data <- data.frame(
EmployeeID = c(101, 102, 103, 104, 105),
Name = c("Alice", "Bob", "Charlie", "David", "Eve"),
Age = c(25, 30, 28, 35, 22),
Department = c("HR", "Finance", "IT", "Marketing", "Sales")
)
# Display the original data frame
print("Original Employee Data:")
print(employee_data)
# Sort the data frame by employee names in ascending order
sorted_employee_data <- employee_data[order(employee_data$Name), ]
print("Sorted Employee Data:")
print(sorted_employee_data)

OUTPUT:
Q.2 ​ Write an R program to create a data frame using two given vectors and display the
duplicated elements and unique rows of the said data frame.

R Code:
# Define the two vectors
vector1 <- c(1, 2, 2, 3, 4, 4, 5)
vector2 <- c('A', 'B', 'B', 'C', 'D', 'D', 'E')

# Create a data frame using the two vectors


data_frame <- data.frame(Column1 = vector1, Column2 = vector2)
# Display the original data frame
print("Original Data Frame:")
print(data_frame)
# Find and display duplicated elements
duplicated_elements <- data_frame[duplicated(data_frame), ]
print("Duplicated Elements:")
print(duplicated_elements)

# Find and display unique rows


unique_rows <- unique(data_frame)
print("Unique Rows:")
print(unique_rows)

OUTPUT:
Q.3 Write an R program to change the first level of a factor with another level of a given factor.
R Code:

# Define a factor
original_factor <- factor(c("low", "medium", "high", "medium", "low", "high"))

# Display the original factor levels


print("Original Factor Levels:")
print(levels(original_factor))

# Define the new level to replace the first level


new_level <- "very_low"
# Change the first level of the factor
levels(original_factor)[1] <- new_level
# Display the modified factor levels
print("Modified Factor Levels:")
print(levels(original_factor))
# Display the modified factor
print("Modified Factor:")
print(original_factor)

OUTPUT:
ASSIGNMENT 4

Q.1 ​ Write a script in R to create a list of cities and perform the following
1.​ Give names to the elements in the list.
2.​ Add an element at the end of the list.
3.​ Remove the last element.
4.​ Update the 3rd Element

R Code:

# Step 1: Create a list of cities


cities <- list("New York", "London", "Tokyo")

# Give names to the elements in the list


names(cities) <- c("City1", "City2", "City3")
cat("List after giving names to elements:\n")
print(cities)

# Step 2: Add an element at the end of the list


cities <- c(cities, City4 = "Paris")
cat("\nList after adding an element at the end:\n")
print(cities)

# Step 3: Remove the last element


cities <- cities[-length(cities)]
cat("\nList after removing the last element:\n")
print(cities)

# Step 4: Update the 3rd Element


cities[[3]] <- "Sydney"
cat("\nList after updating the 3rd element:\n")
print(cities)

OUTPUT:
Q.2 Write a script in R to create two vectors of different lengths and give these vectors as input to
array and print addition and subtraction of those matrices.

R Code:

# Create two vectors of different lengths


vector1 <- c(1, 2, 3, 4, 5, 6)
vector2 <- c(7, 8, 9, 10)
# Define the dimensions of the arrays
dim1 <- c(3, 2) # 3 rows and 2 columns
dim2 <- c(2, 2) # 2 rows and 2 columns
# Create the arrays from the vectors
array1 <- array(vector1, dim = dim1)
array2 <- array(vector2, dim = dim2)
# Print the arrays
cat("Array 1:\n")
print(array1)
cat("\nArray 2:\n")
print(array2)

# Since the arrays are of different dimensions, we need to adjust them


# Let's resize array2 to match array1 by repeating its elements
array2_resized <- array(vector2, dim = dim1)
# Print the resized array2
cat("\nResized Array 2:\n")
print(array2_resized)
# Perform addition of the arrays
addition_result <- array1 + array2_resized
# Perform subtraction of the arrays
subtraction_result <- array1 - array2_resized
# Print the results
cat("\nAddition of the arrays:\n")
print(addition_result)
cat("\nSubtraction of the arrays:\n")
print(subtraction_result)

OUTPUT:
Q.3 Write an R Program to calculate Multiplication Table

R Code:

# Define the number for which the multiplication table is to be printed


number <- 5 # You can change this to any other number

# Print the multiplication table


cat("Multiplication Table for", number, ":\n")
for (i in 1:10) {
cat(number, "x", i, "=", number * i, "\n")
}
OUTPUT:

You might also like