1

I just started to work with R, because I need to do a Lasso regression. To get familiar with the system, I created a very plain matrix with 10 variables and 80 observations each using:

testmatrix<-matrix(rnorm(800),80,10)

I want the 10th variable to be the binary response variable. I already named the 10th variable "responsible_var", and now I would like to transform it into values either 1 (for >1) or 0 (for <1). I used the ifelse function:

testmatrix$responsible_var <- ifelse(testmatrix$responsible_var>0, 1, 0)

But it keeps telling me:

Error in testmatrix$response_var : $ operator is invalid for atomic vectors

What is the problem?

Ruthger Righart
  • 4,799
  • 2
  • 28
  • 33
Moritz
  • 495
  • 1
  • 7
  • 17
  • 1
    You may have to use `testmatrix[,'response_var']` as `$` wont' work with matrix – akrun May 15 '15 at 14:56
  • thanks for the quick reply! use it where: left or right of the "<-" ? – Moritz May 15 '15 at 15:01
  • 2
    In both, i.e. `testmatrix[, 'response_var'] <- ifelse(testmatrix[,'response_var] >0, 1, 0)` Or you can use the numeric index, i.e. `testmatrix[,10] <- ` – akrun May 15 '15 at 15:02
  • @Moritz: I do not know what package you plan to use for your Lasso, but isn't it better to have it anyways in data.frame instead of matrix? Use `testmatrix<-data.frame(matrix(rnorm(800),80,10))` and then `testmatrix[,11] <- ifelse(testmatrix[,10]>0, 1, 0)` will work. – Ruthger Righart May 15 '15 at 15:18
  • I tried the first aproach and what happened is that it adds either "+00" or "+01" to each observation of response_var – Moritz May 15 '15 at 15:21
  • @Moritz I don't know if your comment is directed towards me. I was just copy/pasting your code (changed only the `$` part – akrun May 15 '15 at 15:27
  • Dear Ruthger, it worked indeed. thx to both of you ! – Moritz May 15 '15 at 15:30
  • just one last question: what is technically the difference between a pure matrix and a data.frame(matrix)? – Moritz May 15 '15 at 15:31
  • Everything in a matrix must be the same data type. In a data frame, everything in a column must be the same data type, but different columns can have different data types. See also [data frame or matrix](http://stackoverflow.com/q/5158790/903061). – Gregor Thomas May 15 '15 at 16:01

0 Answers0