Program No.
- 01
Download and install R-Programming environment and install basic packages using
install.packages() command in R.
Ans.
Step 1: Download and Install R Programming Environment
• Visit the official R website: https://cran.r-project.org.
• Choose your operating system (Windows, macOS, or Linux).
• Download the appropriate version of R for your system.
• Follow the installation instructions to complete the setup.
Step 2: Install Basic Packages in R
• Open the R console or RStudio (a popular IDE for R).
• Use the install.packages() function to install basic packages.
• Wait for the installation to complete. The packages will be downloaded from CRAN and
installed in your system.
Step 3: Verify Package Installation
• To check if a package is successfully installed, use the library() command:
If no error message appears, the package has been installed correctly.
Page | 1
Program No. - 02
Learn all the basics of R-Programming (Data types, Variables, Operators etc.)
Ans. Basics of R Programming
1. Data Types in R: R has several data types, which are essential for handling different kinds of data.
• Numeric: Numbers (e.g., 3.14, 42)
• Integer: Whole numbers (e.g., 7L, 100L; note the 'L' suffix)
• Character: Text data (e.g., "Hello", "R Programming")
• Logical: Boolean values (TRUE, FALSE)
• Complex: Complex numbers (e.g., 3+2i)
2. Variables in R: Variables are used to store data values.
• Assigning values to variables:
• Rules for variable names:
o Must start with a letter.
o Cannot contain spaces or special characters (except _ and .).
o Case-sensitive (Var and var are different).
Page | 2
3. Operators in R: R has several types of operators for performing various operations.
• Arithmetic Operators:
• Relational Operators:
• Logical Operators:
• Assignment Operators:
4. Built-in Functions: R comes with many useful built-in functions:
• print(): Displays output.
• class(): Checks the data type.
• length(): Finds the length of a vector.
Page | 3
Program No. - 03
Write a program to find list of even numbers from 1 to n using R-Loops.
Ans.
# Input: User specifies the value of n
n <- as.integer(readline(prompt = "Enter the value of n: "))
# Initialize an empty vector to store even numbers
even_numbers <- c()
# Loop from 1 to n
for (i in 1:n) {
# Check if the number is even
if (i %% 2 == 0) {
# Add the even number to the list
even_numbers <- c(even_numbers, i)
}
}
# Print the list of even numbers
cat("The even numbers from 1 to", n, "are:\n")
print(even_numbers)
Output:
Page | 4
Program No. - 04
Create a function to print squares of numbers in sequence.
Ans.
# Function to calculate and print squares of numbers in a sequence
print_squares <- function(start, end) {
# Loop through the sequence
for (i in start:end) {
square <- i^2
# Print the number and its square
cat("Number:", i, "Square:", square, "\n")
}
}
Output:
Page | 5
Program No. - 05
Write a program to join columns and rows in a data frame using cbind() and rbind() in R.
Ans.
# Create initial data frames
df1 <- data.frame(ID = c(1, 2, 3), Name = c("Alice", "Bob", "Charlie"))
df2 <- data.frame(Age = c(25, 30, 35), City = c("New York", "Los Angeles", "Chicago"))
# Join columns using cbind()
combined_columns <- cbind(df1, df2)
# Print the data frame with combined columns
cat("Data Frame After cbind():\n")
print(combined_columns)
# Create another data frame to add rows
new_rows <- data.frame(ID = c(4, 5), Name = c("David", "Eva"), Age = c(40, 22), City =
c("Houston", "Miami"))
# Join rows using rbind()
combined_rows <- rbind(combined_columns, new_rows)
# Print the data frame with combined rows
cat("\nData Frame After rbind():\n")
print(combined_rows)
Output:
Page | 6
Program No. - 06
Implement different String Manipulation functions in R.
Ans.
# Define a sample string
text <- "R programming is fun!"
# 1. Find the length of a string
length_of_string <- nchar(text)
cat("Length of the string:", length_of_string, "\n")
# 2. Convert string to uppercase
uppercase_text <- toupper(text)
cat("Uppercase:", uppercase_text, "\n")
# 3. Convert string to lowercase
lowercase_text <- tolower(text)
cat("Lowercase:", lowercase_text, "\n")
# 4. Extract substrings
substring_text <- substr(text, start = 3, stop = 14)
cat("Substring (3rd to 14th character):", substring_text, "\n")
# 5. Replace a substring
replaced_text <- sub("fun", "amazing", text)
cat("Text after replacement:", replaced_text, "\n")
# 6. Split a string into words
split_text <- strsplit(text, " ")
cat("Split into words:\n")
print(split_text)
# 7. Concatenate strings
concatenated_text <- paste("Learning", "R", "is fun!", sep = " ")
cat("Concatenated string:", concatenated_text, "\n")
# 8. Trim leading and trailing spaces
text_with_spaces <- " R programming "
trimmed_text <- trimws(text_with_spaces)
cat("Trimmed text:", trimmed_text, "\n")
# 9. Count occurrences of a pattern
pattern_count <- gregexpr("is", text)
matches <- regmatches(text, pattern_count)
cat("Occurrences of 'is':", length(unlist(matches)), "\n")
# 10. Check if a string starts or ends with a specific pattern
starts_with <- startsWith(text, "R")
Page | 7
ends_with <- endsWith(text, "fun!")
cat("Starts with 'R':", starts_with, "\n")
cat("Ends with 'fun!':", ends_with, "\n")
Output:
Page | 8
Program No. - 07
Implement different data structures in R (Vectors, Lists, Data Frames)
Ans.
1. Vectors
# Creating a numeric vector
numeric_vector <- c(10, 20, 30, 40, 50)
cat("Numeric Vector:\n")
print(numeric_vector)
# Creating a character vector
char_vector <- c("Apple", "Banana", "Cherry")
cat("\nCharacter Vector:\n")
print(char_vector)
# Creating a logical vector
logical_vector <- c(TRUE, FALSE, TRUE, TRUE)
cat("\nLogical Vector:\n")
print(logical_vector)
Output:
Page | 9
2. Lists
# Creating a list with different types of elements
my_list <- list(
name = "John Doe",
age = 25,
scores = c(85, 90, 95),
graduated = TRUE
)
cat("\nList:\n")
print(my_list)
# Accessing elements in a list
cat("\nAccessing Name from List:\n")
print(my_list$name)
cat("\nAccessing Scores from List:\n")
print(my_list$scores)
Output:
Page | 10
3. Data Frames
# Creating a data frame
student_data <- data.frame(
ID = c(1, 2, 3),
Name = c("Alice", "Bob", "Charlie"),
Age = c(20, 22, 23),
Score = c(85, 90, 88)
)
cat("\nData Frame:\n")
print(student_data)
# Accessing a column in the data frame
cat("\nAccessing 'Name' column:\n")
print(student_data$Name)
# Adding a new column
student_data$Grade <- c("A", "A+", "B")
cat("\nData Frame after adding a new column:\n")
print(student_data)
Output:
Page | 11
Program No. - 08
Write a program to read a csv file and analyze the data in the file in R.
Ans.
Step 1: Reading the CSV File
# Load necessary library
library(dplyr)
# Read the CSV file
file_path <- readline(prompt = "Enter the path of the CSV file: ")
data <- read.csv(file_path, header = TRUE)
# Print the first few rows of the data
cat("Preview of the data:\n")
print(head(data))
Step 2: Basic Analysis
# 1. Summary of the data
cat("\nSummary of the data:\n")
print(summary(data))
# 2. Structure of the data
cat("\nStructure of the data:\n")
print(str(data))
# 3. Checking for missing values
cat("\nMissing values in each column:\n")
print(colSums(is.na(data)))
# 4. Calculating basic statistics for numeric columns
cat("\nBasic statistics for numeric columns:\n")
numeric_data <- select_if(data, is.numeric)
print(summary(numeric_data))
Step 3: Data Visualization
# Load ggplot2 for visualization
library(ggplot2)
# Example: Histogram of a numeric column (replace 'ColumnName' with an actual column)
if ("ColumnName" %in% names(data)) {
ggplot(data, aes(x = ColumnName)) +
geom_histogram(binwidth = 10, fill = "blue", color = "black") +
labs(title = "Histogram of ColumnName", x = "ColumnName", y = "Frequency")
} else {
cat("\nReplace 'ColumnName' with a numeric column from your dataset.\n")
}
Page | 12
Output:
Page | 13
Program No. - 09
Create pie chart and bar chart using R.
Ans.
1. Pie Chart
# Define data for the pie chart
categories <- c("Category A", "Category B", "Category C", "Category D")
values <- c(25, 35, 20, 20)
# Create a pie chart
cat("\nCreating Pie Chart...\n")
pie(values,
labels = paste(categories, "(", values, "%)", sep = ""),
col = rainbow(length(values)),
main = "Pie Chart Example")
Output:
Page | 14
2. Bar Chart
# Define data for the bar chart
categories <- c("Product A", "Product B", "Product C", "Product D")
sales <- c(50, 75, 60, 90)
# Create a bar chart
cat("\nCreating Bar Chart...\n")
barplot(sales,
names.arg = categories,
col = "skyblue",
main = "Bar Chart Example",
xlab = "Products",
ylab = "Sales",
border = "blue")
Output:
Page | 15
Program No. - 10
Create a data set and do statistical analysis on the data using R.
Ans.
Step 1: Create a Dataset
# Create a data frame
set.seed(123) # Set seed for reproducibility
data <- data.frame(
ID = 1:20,
Age = sample(20:40, 20, replace = TRUE),
Salary = sample(30000:70000, 20, replace = TRUE),
Department = sample(c("HR", "Finance", "IT", "Marketing"), 20, replace = TRUE)
)
# View the dataset
cat("\nDataset:\n")
print(data)
Step 2: Perform Statistical Analysis
# 1. Summary Statistics
cat("\nSummary Statistics:\n")
print(summary(data))
# 2. Mean and Median of Salary
mean_salary <- mean(data$Salary)
median_salary <- median(data$Salary)
cat("\nMean Salary:", mean_salary, "\n")
cat("Median Salary:", median_salary, "\n")
# 3. Standard Deviation and Variance of Salary
std_dev_salary <- sd(data$Salary)
variance_salary <- var(data$Salary)
cat("Standard Deviation of Salary:", std_dev_salary, "\n")
cat("Variance of Salary:", variance_salary, "\n")
# 4. Correlation between Age and Salary
correlation <- cor(data$Age, data$Salary)
cat("\nCorrelation between Age and Salary:", correlation, "\n")
# 5. Frequency Distribution of Department
cat("\nFrequency Distribution of Departments:\n")
print(table(data$Department))
Page | 16
Step 3: Visualize the Dataset
# Histogram of Age
hist(data$Age,
main = "Histogram of Age",
xlab = "Age",
col = "lightblue",
border = "black")
# Boxplot of Salary
boxplot(data$Salary,
main = "Boxplot of Salary",
ylab = "Salary",
col = "lightgreen")
# Barplot of Department Frequencies
dept_freq <- table(data$Department)
barplot(dept_freq,
main = "Barplot of Department Frequencies",
col = "orange",
xlab = "Departments",
ylab = "Frequency")
Output:
Dataset
Statistical Analysis
Page | 17