[R] Error message: $ operator is invalid for atomic vectors
Rui Barradas
ruipbarradas at sapo.pt
Wed Aug 1 15:41:03 CEST 2012
Hello,
Because there's a bug in the way I read the data: data.matrix is not
well used here, it transforms all variables in character vectors and may
cause problems such as
> "9" < "10"
[1] FALSE
> "9" > "10"
[1] TRUE
It was David Winsemius that pointed this out.
As a side effect, data.matrix returns a matrix so '$' is invalid.
Use read.table and both codes are equivalent.
Rui Barradas
Em 01-08-2012 14:28, arun escreveu:
> HI,
>
> The code was working perfectly fine yesterday and today, until half an hour ago. Couldn't find any problems in the code. Still, I am getting error message.
>
> myMatrix <- data.matrix(read.table(text="
> Name Age
> ANTONY 27
> IMRAN 30
> RAJ 22
> NAHAS 32
> GEO 42
> ", header=TRUE))
>
> MinMaxArray <- data.frame(MIN = 25,MAX=35)
> myMatrix[myMatrix$Age<=MinMaxArray$MAX & myMatrix$Age>=MinMaxArray$MIN,]
> #Error in myMatrix$Age : $ operator is invalid for atomic vectors
>
>
> #Then, I tried Rui's code;
> inx <- MinMaxArray[[ "MIN" ]] <= myMatrix[, "Age"] & myMatrix[, "Age"]<= MinMaxArray[[ "MAX" ]]
> myMatrix[ inx , ]
> Name Age
> [1,] 1 27
> [2,] 3 30
> [3,] 4 32
> #Previously, this was also working fine.
>
> Not sure what is happening.
>
> A.K.
More information about the R-help
mailing list