0

I am trying to understand the COERCION in R programming language. When it comes to explicit coercion, it states that we can convert from one class of vectors(considering general object) to another class.

Consider following,

> x <- c(1L, 0L, 3L)
> class(x)
[1] "integer"

So the class of x is integer here. Now we can convert it to logical as,

> as.logical(x)
[1]  TRUE FALSE  TRUE

So now the class of x should be logical as per the coercion, but when I again display the class of x as follows,

> as.logical(x)
[1]  TRUE FALSE  TRUE
> class(x)
[1] "integer"

How it is working? Please help me in understanding. And please correct my sentence "So now the class of x should be logical as per the coercion"

Anil
  • 1,748
  • 8
  • 32
  • 67
  • 5
    in your last call `class(x)`, `x` is still the first one with only integer because you did not assign the result of `as.logical`. Try `class(as.logical(x))` – cderv Nov 04 '18 at 14:27
  • otherwise you can try to override `x` : `x <- as.logical(x)` – Darren Tsai Nov 04 '18 at 15:14
  • @cderv, Thanks, I thought it will assign it back. Got the point now. – Anil Nov 04 '18 at 16:01

0 Answers0