R programming error: Deleting the right column

0 votes

Hi, I'm new to R programming. Below is my code, it works for the first few variables, but when it comes to the last two variables, it enters an infinite loop and gives me the following error: 

Error in if (maxVar > sl) { : missing value where TRUE/FALSE needed

Code:

backwardElimination <-function(training,sl) {
  numVar=length(training)
  funzRegressor = lm(formula = profit ~.,
               data = training)
  p = summary(funzRegressor)$coefficients[,4]
  maxVar = max(p)
  if (maxVar > sl){
    for (j in c(1:numVar)){
      if (maxVar == p[j]) {
        training = training[, -j]
        backwardElimination(training,sl)
      }
    }
  }
  return(summary(funzRegressor))
}

You can refer the rest of my code:

#importing dataset
dataset = read.csv('50_Startups.csv')


# Encoding categorical data
dataset$State = factor(dataset$State,
                         levels = c('New York', 'California', 'Florida'),
                         labels = c(1, 2, 3))

#splitting in train / test set 
library(caTools)
set.seed(123)
split = sample.split(dataset$Profit, SplitRatio = 4/5)
trainingSet = subset(dataset, split == TRUE)
testSet = subset(dataset, split == FALSE)
#Transforming state in dummy variables
trainingSet$State = factor(trainingSet$State)
dummies = model.matrix(~trainingSet$State)
trainingSet = cbind(trainingSet,dummies)
profit = trainingSet$Profit
trainingSet = trainingSet[, -4]
trainingSet = trainingSet[, -4]
trainingSet = cbind(trainingSet,profit)
#calling the function
SL = 0.05
backwardElimination(trainingSet, SL)
Apr 15 in Data Analytics by Sophie may
• 9,430 points
29 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In Data Analytics

0 votes
1 answer

Changing the column type from numeric to factor - R

You can use the as.factor() function for ...READ MORE

answered May 8, 2018 in Data Analytics by Bharani
• 4,550 points
28 views
0 votes
1 answer

Locating row index of a column which has the maximum value - R

which.max(iris$Sepal.Length) This command will give you the desired ...READ MORE

answered May 16, 2018 in Data Analytics by Bharani
• 4,550 points
61 views
0 votes
1 answer

What are the rules to define a variable name in R programming language?

A variable name in R programming language ...READ MORE

answered Sep 7, 2018 in Data Analytics by zombie
• 3,690 points
70 views
0 votes
1 answer

Error: could not find function - R Programming

This error usually occurs when a package ...READ MORE

answered Oct 29, 2018 in Data Analytics by Maverick
• 10,040 points
294 views
0 votes
1 answer

"Error in eval(ei, envir) : object 'RDX2' not found" when trying to source the code in R

This is a very common issue that ...READ MORE

answered Oct 30, 2018 in Data Analytics by Maverick
• 10,040 points
351 views
0 votes
1 answer

Remove NA values from the output in R programming

Edit your code: columnmean <- function(x, removeNA = ...READ MORE

answered Oct 31, 2018 in Data Analytics by Kalgi
• 37,320 points
30 views
0 votes
1 answer

R programming: Unexpected symbol error

Format your code this way: myfunction <- function() ...READ MORE

answered Dec 17, 2018 in Data Analytics by Sophie may
• 9,430 points
162 views
0 votes
1 answer

R programming error

Alright, you can either use gsub to match the ...READ MORE

answered Dec 18, 2018 in Data Analytics by Tyrion anex
• 8,280 points
16 views
0 votes
1 answer

R Programming error in 'fert'

You're using a factor: fert <- factor(c(50,20,10,10,20,50)) levels(fert) #[1] ...READ MORE

answered Dec 27, 2018 in Data Analytics by Sophie may
• 9,430 points
37 views
0 votes
1 answer

R Programming: Market Basket Analysis Error

The basket.sorted() has less than 5 rules. Refer ...READ MORE

answered Feb 11 in Data Analytics by Sophie may
• 9,430 points
88 views