Defensive R Programming
Dr. Colin Gillespie
Jumping Rivers
As an example, using the shortcuts T
/F
for TRUE
/FALSE
TRUE <- 5
# Error in TRUE <- 5 : invalid (do_set) left-hand side to assignment
Suppose we are working out an F-statistic. It would be natural to have
# df is the F-density function
(F <- df(1, 9, 67))
[1] 0.7798
But R treats positive numbers as TRUE
, so
if(F) message("Yer aff yer heid!")
Yer aff yer heid!
F
is now treated as TRUE
!
TRUE
and FALSE
T
and F
TRUE
, use isTRUE()
isTRUE(T)
[1] TRUE
isTRUE(2)
[1] FALSE
T <- 10
isTRUE(T)
[1] FALSE
Defensive R Programming