0% found this document useful (0 votes)
10 views27 pages

R Lab Manual

The document outlines a comprehensive R Programming Lab that covers installation steps, variable assignment, data types, arithmetic and logical operations, matrix manipulations, financial metrics calculations, recursive functions, prime number generation, correlation analysis, and data frame operations. It includes practical examples and code snippets for each topic, demonstrating how to perform various tasks in R. Additionally, it features exercises involving built-in datasets and visualization techniques.

Uploaded by

Nikhil Kannale
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)
10 views27 pages

R Lab Manual

The document outlines a comprehensive R Programming Lab that covers installation steps, variable assignment, data types, arithmetic and logical operations, matrix manipulations, financial metrics calculations, recursive functions, prime number generation, correlation analysis, and data frame operations. It includes practical examples and code snippets for each topic, demonstrating how to perform various tasks in R. Additionally, it features exercises involving built-in datasets and visualization techniques.

Uploaded by

Nikhil Kannale
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/ 27

lOMoARcPSD|50868604

Downloaded by Nikhil Kannale


lOMoARcPSD|50868604

R Programming Lab (BDS306C)

1. Demonstrate the steps for installation of R and R Studio. Perform the following:
a) Assign different type of values to variables and display the type of variable. Assign different
types such as Double, Integer, Logical, Complex and Character and understand the difference
between each data type.
# Double
double_var <- 3.14
print(class(double_var))

# Integer
int_var <- 42L
print(class(int_var))

# Logical
logical_var <- TRUE
print(class(logical_var))

# Complex
complex_var <- 2 + 3i
print(class(complex_var))

# Character
char_var <- "Hello, World!"
print(class(char_var))
Output:

b) Demonstrate Arithmetic and Logical Operations with simple examples.


# Arithmetic operations

Dept Of CSE, DATA SCIENCE

Downloaded by Nikhil Kannale


lOMoARcPSD|50868604

R Programming Lab (BDS306C)

a <- 5
b <- 3
sum_ab <- a + b
diff_ab <- a - b
product_ab <- a * b
quotient_ab <- a / b

# Logical operations
x <- TRUE
y <- FALSE
and_result <- x && y # Logical AND
or_result <- x 11 y # Logical OR
not_x <- !x # Logical NOT

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


print(paste("Difference: ", diff_ab))
print(paste("Product: ", product_ab))
print(paste("Quotient: ", quotient_ab))
print(paste("AND Result: ", and_result))
print(paste("OR Result: ", or_result))
print(paste("NOT x: ", not_x))

Output:

c) Demonstrate generation of sequences and creation of vectors.

Dept Of CSE, DATA SCIENCE 2

Downloaded by Nikhil Kannale


lOMoARcPSD|50868604

R Programming Lab (BDS306C)

# Sequence from 1 to 10
seq_vector <- 1:10
# Sequence from 1 to 10 with a step of 2
seq_vector_step <- seq(1, 10, by = 2)
print(seq_vector)
print(seq_vector_step)
Output:

d) Demonstrate Creation of Matrices


# Create a 3x3 matrix
mat <- matrix(1:9, nrow = 3, ncol = 3)
print(mat)

Output:

e) Demonstrate the Creation of Matrices from Vectors using Binding Function.


# Create two vectors
vec1 <- c(1, 2, 3)
vec2 <- c(4, 5, 6)

# Bind them as rows to create a matrix


mat_from_rows <- rbind(vec1, vec2)

# Bind them as columns to create a matrix


mat_from_cols <- cbind(vec1, vec2)
print(mat_from_rows)
print(mat_from_cols)

Dept Of CSE, DATA SCIENCE

Downloaded by Nikhil Kannale


lOMoARcPSD|50868604

R Programming Lab (BDS306C)

Output:

f) Demonstrate element extraction from vectors, matrices and arrays

# Vectors
vec <- c(10, 20, 30, 40, 50)
element <- vec[3] # Extract the third element

# Matrices
mat <- matrix(1:9, nrow = 3, ncol = 3)
element_mat <- mat[2, 2] # Extract element in the second row and second column

print(paste("Vector Element: ", element))


print(paste("Matrix Element: ", element_mat))

Output:

2. Assess the Financial Statement of an Organization being supplied with 2 vectors of data:
Monthly Revenue and Monthly Expenses for the Financial Year. You can create your own
sample data vector for this experiment)
Calculate the following financial metrics:
a. Profit for each month.

Dept Of CSE, DATA SCIENCE 4

Downloaded by Nikhil Kannale


lOMoARcPSD|50868604

R Programming Lab (BDS306C)

b. Profit after tax for each month (Tax Rate is 30%).


c. Profit margin for each month equals to profit after tax divided by revenue.
d. Good Months – where the profit after tax was greater than the mean for the year.
e. Bad Months – where the profit after tax was less than the mean for the year.
f. The best month – where the profit after tax was max for the year.
g. The worst month – where the profit after tax was min for the year.

# Sample data for monthly revenue and monthly expenses (in thousands of dollars)
monthly_revenue <- c(100, 120, 130, 110, 140, 160, 150, 130, 140, 170, 180, 160)
monthly_expenses <- c(70, 80, 85, 75, 90, 100, 95, 80, 85, 95, 100, 90)

a. Profit for each month.


# Calculate profit for each month
profit <- monthly_revenue - monthly_expenses

b. Profit after tax for each month (Tax Rate is 30%).


# Tax Rate (30%)
tax_rate <- 0.30
# Calculate profit after tax for each month
profit_after_tax <- profit * (1 - tax_rate)

c. Profit margin for each month equals to profit after tax divided by revenue.
# Calculate profit margin for each month (profit after tax divided by revenue)
profit_margin <- (profit_after_tax / monthly_revenue) * 100

d. Good Months – where the profit after tax was greater than the mean for the year.
# Calculate the mean profit after tax for the year
mean_profit_after_tax <- mean(profit_after_tax)

# Find the good months (where profit after tax was greater than the mean for the year)
good_months <- which(profit_after_tax > mean_profit_after_tax)

Dept Of CSE, DATA SCIENCE

Downloaded by Nikhil Kannale


lOMoARcPSD|50868604

R Programming Lab (BDS306C)

e. Bad Months – where the profit after tax was less than the mean for the year.
# Find the bad months (where profit after tax was less than the mean for the year)
bad_months <- which(profit_after_tax < mean_profit_after_tax)

f. The best month – where the profit after tax was max for the year.
# Find the best month (where profit after tax was maximum for the year)
best_month <- which.max(profit_after_tax)

g. The worst month – where the profit after tax was min for the year.
# Find the worst month (where profit after tax was minimum for the year)
worst_month <- which.min(profit_after_tax)

# Display the results


cat("Profit for each month:\n", profit, "\n")
cat("Profit after tax for each month:\n", profit_after_tax, "\n")
cat("Profit margin for each month:\n", profit_margin, "\n")
cat("Good Months:\n", good_months, "\n")
cat("Bad Months:\n", bad_months, "\n")
cat("Best Month:\n", best_month, "\n")
cat("Worst Month:\n", worst_month, "\n")

Output:

Dept Of CSE, DATA SCIENCE 6

Downloaded by Nikhil Kannale


lOMoARcPSD|50868604

R Programming Lab (BDS306C)

3. Develop a program to create two 3 X 3 matrices A and B and perform the following
operations
a) Transpose of the matrix b) addition
c) subtraction d) multiplication

# Create Matrix A
A <- matrix(1:9, nrow = 3, byrow = TRUE)
cat("Matrix A:\n")
print(A)

# Create Matrix B
B <- matrix(10:18, nrow = 3, byrow = TRUE)
cat("Matrix B:\n")
print(B)

# Transpose of Matrix A
cat("Transpose of Matrix A:\n")
print(t(A))

# Addition of Matrices A and B


cat("Addition of Matrices A and B:\n")
print(A + B)

# Subtraction of Matrices A and B


cat("Subtraction of Matrices A and B:\n")
print(A - B)

# Multiplication of Matrices A and B (element-wise)


cat("Element-wise Multiplication of Matrices A and B:\n")

Dept Of CSE, DATA SCIENCE

Downloaded by Nikhil Kannale


lOMoARcPSD|50868604

R Programming Lab (BDS306C)

print(A * B)

# Matrix Multiplication of A and B


cat("Matrix Multiplication of Matrices A and B:\n")
print(A %*% B)

Output:

Dept Of CSE, DATA SCIENCE 8

Downloaded by Nikhil Kannale


lOMoARcPSD|50868604

R Programming Lab (BDS306C)

4. Develop a program to find the factorial of given number using recursive function calls.
factorial <- function(n) {
if(n <= 1) {
return(1)
} else {
return(n * factorial(n-1))
}
}
input <- readline(prompt = "Enter an integer number: ")
# Convert the input to an integer
n <- as.integer(input)
fact<-factorial(n)
cat('Factorial of the given number is',fact)

Output:
Enter an integer number: 5
Factorial of the given number is 120

5. Develop an R Program using functions to find all the prime numbers up to a specified number
by the method of Sieve of Eratosthenes.
sieve <- function(n)
{
n <- as.integer(n)
#if(n > 1e6) stop("n too large")
primes <- rep(TRUE, n)
primes[1] <- FALSE
last.prime <- 2
for(i in last.prime:floor(sqrt(n)))
{

Dept Of CSE, DATA SCIENCE

Downloaded by Nikhil Kannale


lOMoARcPSD|50868604

R Programming Lab (BDS306C)

primes[seq.int(2*last.prime, n, last.prime)] <- FALSE


last.prime <- last.prime + min(which(primes[(last.prime+1):n]))
}
which(primes)
}

sieve(100)

Output:

6. The built-in data set mammals contain data on body weight versus brain weight. Develop R
commands to:
a) Find the Pearson and Spearman correlation coefficients. Are they similar?
b) Plot the data using the plot command.
c) Plot the logarithm (log) of each variable and see if that makes a difference.

install.packages("MASS")
library(MASS)
# Load the dataset
data("mammals")
mammals_clean <- mammals[complete.cases(mammals), ]
# View the first few rows of the dataset
head(mammals_clean)

# a) Find the Pearson and Spearman correlation coefficients


# Pearson correlation
pearson_corr <- cor(mammals_clean$brain, mammals_clean$body, method = "pearson")
cat("Pearson Correlation Coefficient:", pearson_corr, "\n")

Dept Of CSE, DATA SCIENCE 10

Downloaded by Nikhil Kannale


lOMoARcPSD|50868604

R Programming Lab (BDS306C)

# Spearman correlation
spearman_corr <- cor(mammals_clean$brain, mammals_clean$body, method = "spearman")
cat("Spearman Correlation Coefficient:", spearman_corr, "\n")
# b) Plot the data using the plot command
plot(mammals_clean$body, mammals_clean$brain, main = "Body Weight vs. Brain Weight",
xlab = "Body Weight (g)", ylab = "Brain Weight (g)", pch = 19)

# c) Plot the logarithm (log) of each variable


log_body <- log(mammals_clean$body)
log_brain <- log(mammals_clean$brain)
plot(log_body, log_brain, main = "Logarithm of Body Weight vs. Brain Weight",
xlab = "Log of Body Weight", ylab = "Log of Brain Weight", pch = 19)
Output:

Pearson Correlation Coefficient: 0.9341638


Spearman Correlation Coefficient: 0.9534986

b) Plot the data using the plot command

Dept Of CSE, DATA SCIENCE

Downloaded by Nikhil Kannale


lOMoARcPSD|50868604

R Programming Lab (BDS306C)

c) Plot the logarithm (log) of each variable

7. Develop R program to create a Data Frame with following details and do the following
operations.

Dept Of CSE, DATA SCIENCE 12

Downloaded by Nikhil Kannale


lOMoARcPSD|50868604

R Programming Lab (BDS306C)

a) Subset the Data frame and display the details of only those items whose price is greater than
or equal to 350.
b) Subset the Data frame and display only the items where the category is either “Office
Supplies” or “Desktop Supplies”
c) Create another Data Frame called “item-details” with three different fields itemCode,
ItemQtyonHand and ItemReorderLvl and merge the two frames

# Create a data frame with item details


item_data <- data.frame(
itemCode = c("A123", "B456", "C789", "D101", "E202"),
itemName = c("Pen Set", "Laptop", "Chair", "Notepad", "Printer"),
category = c("Office Supplies", "Electronics", "Furniture", "Office Supplies", "Electronics"),
price = c(250, 1200, 400, 5, 300),
ItemQtyonHand = c(100, 25, 50, 200, 10),
ItemReorderLvl = c(20, 5, 10, 100, 3)
)
# a) Subset the Data frame and display items with price greater than or equal to 350
subset_1 <- item_data[item_data$price >= 350, ]
cat("Items with price greater than or equal to 350:\n")
print(subset_1)

# b) Subset the Data frame and display items with category "Office Supplies" or "Desktop
Supplies"

Dept Of CSE, DATA SCIENCE

Downloaded by Nikhil Kannale


lOMoARcPSD|50868604

R Programming Lab (BDS306C)

subset_2 <- item_data[item_data$category %in% c("Office Supplies", "Desktop Supplies"), ]


cat("Items in 'Office Supplies' or 'Desktop Supplies' category:\n")
print(subset_2)

# c) Create another Data Frame called "item-details" with three fields and merge the two
frames
item_details <- data.frame(
itemCode = c("A123", "B456", "C789", "D101", "E202"),
ItemQtyonHand = c(100, 25, 50, 200, 10),
ItemReorderLvl = c(20, 5, 10, 100, 3)
)

# Merge the two data frames on itemCode


merged_data <- merge(item_data, item_details, by = "itemCode")
cat("Merged Data Frame 'merged_data':\n")
print(merged_data)

Output:

Dept Of CSE, DATA SCIENCE 14

Downloaded by Nikhil Kannale


lOMoARcPSD|50868604

R Programming Lab (BDS306C)

8. Let us use the built-in dataset air quality which has Daily air quality measurements in New
York, May to September 1973. Develop R program to generate histogram by using appropriate
arguments for the following statements.
a) Assigning names, using the air quality data set.
b) Change colors of the Histogram
c) Remove Axis and Add labels to Histogram
d) Change Axis limits of a Histogram
e) Add Density curve to the histogram

# Load the airquality dataset


data(airquality)

# a) Assigning names, using the air quality data set.


colnames(airquality) <- c("Ozone", "Solar_R", "Wind", "Temp", "Month", "Day")

# b) Change colors of the Histogram


hist(airquality$Ozone, col = "skyblue", main = "Histogram of Ozone Levels", xlab = "Ozone

Dept Of CSE, DATA SCIENCE

Downloaded by Nikhil Kannale


lOMoARcPSD|50868604

R Programming Lab (BDS306C)

Concentration")

# c) Remove Axis and Add labels to Histogram


hist(airquality$Solar_R, col = "lightgreen", main = "Solar Radiation Levels", xlab = "Solar
Radiation", ylab = "Frequency", axes = FALSE)
axis(1, at = pretty(airquality$Solar_R), labels = pretty(airquality$Solar_R))

# d) Change Axis limits of a Histogram


hist(airquality$Wind, col = "salmon", main = "Wind Speed Levels", xlab = "Wind Speed", xlim =
c(0, 20))

# e) Add Density curve to the histogram


hist(airquality$Temp, col = "lightcoral", main = "Temperature Levels", xlab = "Temperature")
lines(density(airquality$Temp), col = "blue", lwd = 2)

Output:

Dept Of CSE, DATA SCIENCE 16

Downloaded by Nikhil Kannale


lOMoARcPSD|50868604

R Programming Lab (BDS306C)

Dept Of CSE, DATA SCIENCE

Downloaded by Nikhil Kannale


lOMoARcPSD|50868604

R Programming Lab (BDS306C)

9. Design a data frame in R for storing about 20 employee details. Create a CSV file named
“input.csv” that defines all the required information about the employee such as id, name,
salary, start_date, dept. Import into R and do the following analysis.
a) Find the total number rows & columns
b) Find the maximum salary
c) Retrieve the details of the employee with maximum salary
d) Retrieve all the employees working in the IT Department.
e) Retrieve the employees in the IT Department whose salary is greater than 20000 and write
these details into another file “output.csv”

# Create a data frame for employee details


employee_data <- data.frame(
id = 1:20,
name = c("John", "Jane", "Alice", "Bob", "Charlie", "David", "Emma", "Frank", "Grace", "Helen",
"Ivy", "Jack", "Karen", "Leo", "Mary", "Nina", "Oliver", "Peter", "Quinn", "Rachel"),
salary = c(25000, 30000, 35000, 40000, 45000, 50000, 55000, 60000, 65000, 70000,
75000, 80000, 85000, 90000, 95000, 100000, 105000, 110000, 115000, 120000),
start_date = as.Date(c("2022-01-10", "2021-11-05", "2020-08-15", "2023-03-20", "2019-12-
25",
"2021-06-30", "2022-05-12", "2020-02-18", "2023-09-08", "2022-07-01",

Dept Of CSE, DATA SCIENCE 18

Downloaded by Nikhil Kannale


lOMoARcPSD|50868604

R Programming Lab (BDS306C)

"2022-03-15", "2021-04-22", "2023-02-09", "2020-10-17", "2019-07-03",


"2021-09-14", "2020-06-28", "2023-01-05", "2019-05-21", "2022-11-10")),
dept = c("HR", "IT", "IT", "Finance", "IT", "HR", "Finance", "IT", "HR", "IT",
"Finance", "HR", "IT", "Finance", "IT", "HR", "Finance", "IT", "HR", "IT")
)

# Create a CSV file


write.csv(employee_data, "input.csv", row.names = FALSE)

# Read the CSV file into R


employee_data <- read.csv("input.csv")

# a) Find the total number of rows and columns


num_rows <- nrow(employee_data)
num_columns <- ncol(employee_data)

cat("Total number of rows:", num_rows, "\n")


cat("Total number of columns:", num_columns, "\n")

# b) Find the maximum salary


max_salary <- max(employee_data$salary)
cat("Maximum salary:", max_salary, "\n")

# c) Retrieve the details of the employee with the maximum salary


employee_with_max_salary <- employee_data[employee_data$salary == max_salary, ]
cat("Details of employee with maximum salary:\n")
print(employee_with_max_salary)
# d) Retrieve all the employees working in the IT Department
it_department_employees <- employee_data[employee_data$dept == "IT", ]
cat("Employees working in the IT Department:\n")
print(it_department_employees)

Dept Of CSE, DATA SCIENCE

Downloaded by Nikhil Kannale


lOMoARcPSD|50868604

R Programming Lab (BDS306C)

# e) Retrieve the employees in the IT Department whose salary is greater than 20000
it_department_high_salary <- it_department_employees[it_department_employees$salary >
20000, ]

# Write these details into another file "output.csv"


write.csv(it_department_high_salary, "output.csv", row.names = FALSE)

Output:

10. Using the built in dataset mtcars which is a popular dataset consisting of the design and fuel
consumption patterns of 32 different automobiles. The data was extracted from the 1974
Motor Trend US magazine, and comprises fuel consumption and 10 aspects of automobile
design and performance for 32 automobiles (1973-74 models). Format A data frame with 32
observations on 11 variables :
[1] mpg Miles/(US) gallon, [2] cyl Number of cylinders [3] disp Displacement (cu.in.),
[4] hp Gross horsepower [5] drat Rear axle ratio, [6] wt Weight (lb/1000)
[7] qsec 1/4 mile time, [8] vs V/S, [9] am Transmission (0 = automatic, 1 = manual),
[10] gear Number of forward gears, [11] carb Number of carburetors

Develop R program, to solve the following:


a) What is the total number of observations and variables in the dataset?

Dept Of CSE, DATA SCIENCE 20

Downloaded by Nikhil Kannale


lOMoARcPSD|50868604

R Programming Lab (BDS306C)

b) Find the car with the largest hp and the least hp using suitable functions
c) Plot histogram / density for each variable and determine whether continuous variables are
normally distributed or not. If not, what is their skewness?
d) What is the average difference of gross horse power(hp) between automobiles with 3 and 4
number of cylinders(cyl)? Also determine the difference in their standard deviations.
e) Which pair of variables has the highest Pearson correlation?

install.packages("moments")
library(moments)
# Load the mtcars dataset
data(mtcars)
# a) What is the total number of observations and variables in the dataset?
num_observations <- nrow(mtcars)
num_variables <- ncol(mtcars)

cat("Total number of observations:", num_observations, "\n")


cat("Total number of variables:", num_variables, "\n")

# b) Find the car with the largest hp and the least hp using suitable functions
car_with_max_hp <- mtcars[which.max(mtcars$hp), ]
car_with_min_hp <- mtcars[which.min(mtcars$hp), ]

cat("Car with the largest hp:\n")


print(car_with_max_hp)

cat("Car with the least hp:\n")


print(car_with_min_hp)

# c) Plot histogram / density for each variable and determine whether continuous variables are
normally distributed or not.
# If not, what is their skewness?
par(mfrow = c(4, 3)) # Setting up a 4x3 grid for plots

Dept Of CSE, DATA SCIENCE

Downloaded by Nikhil Kannale


lOMoARcPSD|50868604

R Programming Lab (BDS306C)

for (col in colnames(mtcars)) {


hist(mtcars[, col], main = col, xlab = "")
if (is.numeric(mtcars[, col])) {
cat(paste(col, "skewness:", skewness(mtcars[, col]), "\n"))
}
}

# d) What is the average difference of gross horsepower (hp) between automobiles with 3 and
4 cylinders (cyl)?
# Also determine the difference in their standard deviations.
average_hp_diff <- mean(mtcars[mtcars$cyl == 3, "hp"]) - mean(mtcars[mtcars$cyl == 4, "hp"])
std_deviation_diff <- sd(mtcars[mtcars$cyl == 3, "hp"]) - sd(mtcars[mtcars$cyl == 4, "hp"])

cat("Average hp difference between 3 and 4 cylinder cars:", average_hp_diff, "\n")


cat("Standard deviation difference between 3 and 4 cylinder cars:", std_deviation_diff, "\n")

# e) Which pair of variables has the highest Pearson correlation?


cor_matrix <- cor(mtcars)
diag(cor_matrix) <- 0 # Set diagonal elements to 0 to avoid self-correlation
max_corr_value <- max(cor_matrix)
max_corr_vars <- which(cor_matrix == max_corr_value, arr.ind = TRUE)

cat("Pair of variables with the highest Pearson correlation:\n")


cat(colnames(mtcars)[max_corr_vars[1, 2]], "and", colnames(mtcars)[max_corr_vars[1, 1]],
"with correlation", max_corr_value, "\n")

Output:

Dept Of CSE, DATA SCIENCE 22

Downloaded by Nikhil Kannale


lOMoARcPSD|50868604

R Programming Lab (BDS306C)

Dept Of CSE, DATA SCIENCE

Downloaded by Nikhil Kannale


lOMoARcPSD|50868604

R Programming Lab (BDS306C)

11. Demonstrate the progression of salary with years of experience using a suitable data set
(You can create your own dataset). Plot the graph visualizing the best fit line on the plot of
the given data points. Plot a curve of Actual Values vs. Predicted values to show their
correlation and performance of the model. Interpret the meaning of the slope and y-intercept
of the line with respect to the given data. Implement using lm function. Save the graphs and
coefficients in files. Attach the predicted values of salaries as a new column to the original
data set and save the data as a new CSV file.

# Step 1: Create a dataset

Dept Of CSE, DATA SCIENCE 24

Downloaded by Nikhil Kannale


lOMoARcPSD|50868604

R Programming Lab (BDS306C)

years_of_experience <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)


salary <- c(30000, 35000, 40000, 45000, 50000, 55000, 60000, 65000, 70000, 75000)
data <- data.frame(Experience = years_of_experience, Salary = salary)

# Step 2: Fit a linear regression model


model <- lm(Salary ~ Experience, data=data)

# Step 3: Generate predicted values


predicted <- predict(model)

# Step 4: Create scatter plots and save them


png("scatter_plot.png")
plot(data$Experience, data$Salary, main="Actual vs. Predicted Salary", xlab="Years of
Experience", ylab="Salary", pch=19, col="blue")
abline(model, col="red")
dev.off()
# Step 5: Save coefficients to a file
coefficients <- summary(model)$coefficients
write.table(coefficients, "coefficients.txt")

# Step 6: Attach predicted values and save as a new CSV file


data$Predicted_Salary <- predicted
write.csv(data, "salary_data_with_predictions.csv")

# Print the coefficients


cat("Linear Regression Coefficients:\n")
print(coefficients)

# Interpret the meaning of the slope and y-intercept


cat("\nInterpretation:\n")
cat("The slope (coefficient for Experience) represents the change in salary for each
additional year of experience. In this case, it indicates how much salary increases with each

Dept Of CSE, DATA SCIENCE

Downloaded by Nikhil Kannale


lOMoARcPSD|50868604

R Programming Lab (BDS306C)

additional year of experience.\n")


cat("The y-intercept (coefficient for the intercept term) represents the estimated salary
when years of experience is zero. It may not have a meaningful interpretation in this
context, as no one has zero years of experience.")

Output:

Interpretation:

The slope (coefficient for Experience) represents the change in salary for ea
ch additional year of experience. In this case, it indicates how much salary
increases with each additional year of experience.

The y-intercept (coefficient for the intercept term) represents the estimated
salary when years of experience is zero. It may not have a meaningful interpr
etation in this context, as no one has zero years of experience.

salary_data_with_predictions.csv

Dept Of CSE, DATA SCIENCE 26

Downloaded by Nikhil Kannale

You might also like