Reputation: 739
For instance:
> TRUE * 0.5
0.5
> FALSE * 0.5
0
I don't know if the secret here is the * character itself or the way R encodes logical statements, but I can't understand why the results.
Upvotes: 0
Views: 780
Reputation: 263411
R has a fairly loose type system and rather freely does coercion, hopefully when it is sensible. When coerced to numeric by *
, logical values become 0 (FALSE) and 1 (TRUE), your expression gets evaluated with the usual mathematical convention of all values times 0 equal 0, and all values times 1 equal the value. The one exception to that rule in the numeric domain is Inf * 0
returns NaN
. Character values have no "destination"-type when composed with "*", so "1"*TRUE
throws an error.
Upvotes: 3