Marjolein Straathof
Marjolein Straathof

Reputation: 43

Error in I[j] <- IFunction(j, d, x) : object of type 'closure' is not subsettable

I am keep getting an error for this small problem below:

assignment5<- read.csv(file="C:/Users/Marjolein/Desktop/assignment5data.csv",header=TRUE,se p=";")
d <- as.vector(assignment5[["demand"]])

x<-400
n <- 1461

IFunction <- function (j,d,x){
if (d[j] <= x)
    {
    I <- 1
    } else 
      {
      I <- 0
      }
      return(I)
}

for (j in 1:(n)){
  I[j] <- IFunction(j,d,x)
  I
}

The error is: Error in I[j] <- IFunction(j, d, x) : 
  object of type 'closure' is not subsettable

So I guess there is something wrong with; I[j] <- IFunction(j,d,x). since now it sees I as a function, but it should be seen as a value

Is there someone who can help me?

With kind regards,

Marjolein straathof

Upvotes: 0

Views: 87

Answers (1)

Jason
Jason

Reputation: 2607

I is a function. Pick another variable name.

You can test this by typing the variable names, and seeing what happens:

> I

 function (x) 
 {
     structure(x, class = unique(c("AsIs", oldClass(x))))
 }

> J

Error: object 'J' not found

That 'object not found' is good news for you: it's not being used by anything else. You'll need to let R know what you want to do with it before you start using it like a vector. NA is a good choice to initialize something:

J <- NA
for (j in 1:(n)){
  J[j] <- IFunction(j,d,x)
  J
}

Upvotes: 1

Related Questions