Statistics With R
Statistics With R
1. Create an R program that takes the user's name as input and prints a
personalized greeting.
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))
7. Generate a basic bar plot illustrating the marks for five different subjects.
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 <- data.frame(
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 = "…")