Directorate of Online Education
SRM Institute of Science and Technology
SRM Nagar, Kattankulathur-603203
STATISTICS WITH R - LAB MANUAL
1. Create an R program that takes the user's name as input and prints a
personalized greeting.
# Question 1: Hello User
cat("Enter your name: ")
user_name <- readline()
cat("Hello, ", user_name, "! Nice to meet you.\n")
2. Write an R program that calculates the sum of cube for a given vector of
numbers.
# Question 2: Sum of Squares
numbers <- c(2, 4, 6, 8, 10)
sum_of_cube <- sum(numbers^3)
cat("Original Vector: ", numbers, "\n")
cat("Sum of Squares: ", sum_of_squares, "\n")
3. Write a R program to create a vector and find the maximum and the
minimum value of a created vector.
x = c(10, 20, 30, 25, 9, 26)
print("Original Vectors:")
print(x)
print("Maximum value of the above Vector:")
print(max(x))
print("Minimum value of the above Vector:")
print(min(x))
4. Write a R program to find Sum, Mean and Product of a Vector.
R Programming Code :
x = c(10, 20, 30)
print("Sum:")
print(sum(x))
print("Mean:")
print(mean(x))
print("Product:")
print(prod(x))
5. Write a R program to combine two given vectors by columns, rows.
R Programming Code :
v1 = c(1,3,5,7,9)
v2 = c(2,4,6,8,10)
print("Original vectors:")
print(v1)
print(v2)
print("Combines the said two vectors by columns:")
result = cbind(v1,v2)
print(result)
print("Combines the said two vectors by rows:")
result = rbind(v1,v2)
print(result)
6. Write an R program that calculates the sum of numbers from 1 to 10
using a for loop.
# Question: Sum of Numbers
sum_result <- 0
for (i in 1:10) {
sum_result <- sum_result + i
}
cat("Sum of numbers from 1 to 10 is: ", sum_result, "\n")
7. Generate a basic bar plot illustrating the marks for five different subjects.
marks = c(70, 95, 80, 74,82)
barplot(marks,
main = "Comparing marks of 5 subjects",
xlab = "Marks",
ylab = "Subject",
[Link] = c("English", "Science", "Math.", "Hist.", “Lang”),
col = "darkred", horiz = FALSE)
8. DataFrame Creation:
(i) Create a dataframe named employee_data with columns for employeeID,
Name, Age, and Designation.
(ii) Populate the dataframe with information for five employees.
(iii) Display the created dataframe.
# Create a Dataframe
employee_data <- [Link](
employeeID = c(1, 2, 3, 4, 5),
Name = c("…", "…", "…", "…", "…"),
Age = c(22, 23, 21, 22, 24),
Designation = c("…", "…", "…", "…", "…")
)
# Display the Dataframe
print(employee_data)
9. Create an R program that checks whether a given number is odd or even.
# Odd or Even
check_odd_even <- function(number) {
if (number %% 2 == 0) {
return("Even")
} else {
return("Odd")
}
}
# Test the function
test_number <- 15
result <- check_odd_even(test_number)
cat(test_number, " is ", result, "\n")
10. Write R Program to import dataset from R and execute the following
R Programming Code :
(i) display the first and last 6 rows of the dataset using R function.
(ii) find the summary of the dataset.
(iii) plot a histogram for one continuous variable
# Import the dataset
data(dataset)
# Display the first and last 6 rows of the dataset
print("First 6 Rows:")
print(head(dataset))
print("\nLast 6 Rows:")
print(tail(dataset))
# Find the summary of the dataset
print("\nSummary of the Dataset:")
print(summary(dataset))
# Plot a histogram for one continuous variable (e.g., mpg)
hist(dataset$variable, main = "Histogram", xlab = "…", col = "…", border = "…")