thecheech
thecheech

Reputation: 2211

as.integer(vector) changes the vector, even though it contains only round values (in r)

How come a when I transform a vector of 0s and 1s in r it deforms the vector and changes its values?

table(Prediction)

Prediction
  0   1 
318 133

as.integer(Prediction)

  [1] 2 1 1 2 1 2 2 1 2 1 1 1 1 1 1 1 1 2 2 1 1 1 1 1 1 1 1 1 1 2 1 1 2 2 2 1 1 1 1 2 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 2 1 2 1 1 2 2 1 2 1 2 1 1 1 1 1 1 2 1 1 2 1 2 1 2 1 1 2 1 1 2 1 1 1 2 1 1 2 1 2 1 2 1 2 2
[102] 1 1 1 1 1 2 1 1 2 1 1 1 1 2 1 2 1 2 2 1 1 1 1 2 1 1 1 1 1 1 2 1 2 1 1 2 2 2 1 2 2 1 1 1 2 1 1 1 1 1 2 1 2 1 1 2 1 1 1 2 1 1 1 1 1 1 1 2 2 1 1 2 1 1 2 1 2 2 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 2 1 1 1 1 1 2
[203] 1 1 2 1 1 1 1 2 1 2 1 2 2 1 1 2 1 1 1 1 1 1 1 1 1 1 1 2 2 1 1 1 1 1 1 2 1 1 2 1 1 1 1 1 1 1 1 2 1 2 1 1 1 1 1 2 1 1 2 1 2 1 1 1 2 1 2 1 2 1 1 1 1 1 2 2 1 1 2 1 1 1 1 1 2 2 1 2 2 1 1 1 1 1 1 1 1 1 1 1 2
[304] 1 1 1 1 2 1 1 2 1 1 1 2 2 1 1 1 2 1 1 1 2 1 2 1 2 1 1 1 1 2 1 2 1 1 2 1 2 2 2 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 2 1 2 2 1 1 1 1 2 1 2 1 1 1 1 1 1 2 1 1 1 2 1 1 2 1 1 1 2 1 2 2 2 1 1 1 2 1 1 1 1 1 1 2 1
[405] 1 1 1 1 1 2 1 1 2 2 2 2 2 1 2 1 1 1 2 1 1 2 2 1 1 1 1 2 1 1 2 2 1 1 1 2 2 1 2 1 1 2 1 2 2 1 1

Upvotes: 0

Views: 78

Answers (1)

Sven Hohenstein
Sven Hohenstein

Reputation: 81683

I suppose Prediction is a factor:

> Prediction <- factor(c(0, 0, 1, 1))
> table(Prediction)
Prediction
0 1 
2 2 
> as.integer(Prediction)
[1] 1 1 2 2

Maybe you want:

> as.integer(as.character(Prediction))
[1] 0 0 1 1

Upvotes: 4

Related Questions