R code not running

here is the code that im trying to run:

Simple Linear Regression

Importing the dataset

dataset = read.csv('Salary_Data.csv')

Splitting the dataset into the Training set and Test set

install.packages('caTools')

library(caTools)
set.seed(123)
split = sample.split(dataset$Salary, SplitRatio = 2/3)
training_set = subset(dataset, split == TRUE)
test_set = subset(dataset, split == FALSE)

Feature Scaling

training_set = scale(training_set)

test_set = scale(test_set)

Fitting Simple Linear Regression to the Training set

regressor = lm(formula = Salary ~ YearsExperience,
data = training_set)

Predicting the Test set results

y_pred = predict(regressor, newdata = test_set)

Visualising the Training set results

library(ggplot2)
ggplot() +
geom_point(aes(x = training_set$YearsExperience, y = training_set$Salary),
colour = 'red') +
geom_line(aes(x = training_set$YearsExperience, y = predict(regressor, newdata = training_set)),
colour = 'blue') +
ggtitle('Salary vs Experience (Training set)') +
xlab('Years of experience') +
ylab('Salary')

Visualising the Test set results

library(ggplot2)
ggplot() +
geom_point(aes(x = test_set$YearsExperience, y = test_set$Salary),
colour = 'red') +
geom_line(aes(x = training_set$YearsExperience, y = predict(regressor, newdata = training_set)),
colour = 'blue') +
ggtitle('Salary vs Experience (Test set)') +
xlab('Years of experience') +
ylab('Salary')
here is the messege that appears in consol when im running the code :
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
cannot open file 'Salary_Data.csv': No such file or directory

It looks like you need to specify the path to the CSV file.

im sorry but how can i do that

Change dataset = read.csv('Salary_Data.csv') to dataset = read.csv("C:/.../Salary_Data.csv") where "..." is the rest of the file path (assuming you are on Windows; adjust accordingly for MacOS or Linux).

1 Like