R Practical File
R Practical File
- 01
Download and install R-Programming environment and install basic packages using
install.packages() command in R.
Ans.
• Wait for the installation to complete. The packages will be downloaded from CRAN and
installed in your system.
Step 3: Verify Package Installation
Page | 1
Program No. - 02
Learn all the basics of R-Programming (Data types, Variables, Operators etc.)
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)
Page | 2
3. Operators in R: R has several types of operators for performing various operations.
• Arithmetic Operators:
• Relational Operators:
• Logical Operators:
• Assignment Operators:
Page | 3
Program No. - 03
Ans.
# 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)
}
}
Output:
Page | 4
Program No. - 04
Ans.
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.
Output:
Page | 6
Program No. - 06
Ans.
# 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")
# 7. Concatenate strings
concatenated_text <- paste("Learning", "R", "is fun!", sep = " ")
cat("Concatenated string:", concatenated_text, "\n")
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
Ans.
1. Vectors
# Creating a numeric vector
numeric_vector <- c(10, 20, 30, 40, 50)
cat("Numeric Vector:\n")
print(numeric_vector)
Output:
Page | 9
2. Lists
cat("\nList:\n")
print(my_list)
Output:
Page | 10
3. Data Frames
cat("\nData Frame:\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.
Page | 12
Output:
Page | 13
Program No. - 09
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)
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)
Output:
Page | 15
Program No. - 10
Ans.
# 1. Summary Statistics
cat("\nSummary Statistics:\n")
print(summary(data))
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")
Output:
Dataset
Statistical Analysis
Page | 17