Jatt
Jatt

Reputation: 785

Getting incorrect number of dimensions even when the column number exists

I am hitting a peculiar problem. I have a data frame in which I need to replace all 3's and 5's with 0's and 1's respectively.

So I do:

labels = ifelse(labels[1,] == 3, 1, 0)

The number of rows before and after the operation is 1 and number of columns before and after the operation is 11552.

Now I pass this to a function which is given below:

train <- function(data, labels) {
  #browser()

  shuffle <- sample(ncol(labels))
  labels <- labels[,shuffle]
  data <- data[,shuffle]
  for(i in seq(1:ncol(data))) {
    ## .. something here ..  
    y = labels[1,i] #LINE THAT THROWS ERROR -LINE 1
    ## ..something .. 
  }
  return(theta)
}

The marked line LINE 1 throws an error saying:

Error in labels[1, i] : incorrect number of dimensions

In the function above data and labels have the same number of columns. What could be the reason for this error?

Upvotes: 0

Views: 59

Answers (1)

Andrew Gustar
Andrew Gustar

Reputation: 18435

Your first statement turns labels into a vector, so you can't access it in two dimensions. Just use labels[i].

Upvotes: 1

Related Questions