Is There Anything Wrong with Using T & F Instead of True & False

Is there anything wrong with using T & F instead of TRUE & FALSE?

T and F can be re-defined, but TRUE and FALSE are reserved words and cannot be re-defined.

> TRUE <- 1L
Error in TRUE <- 1L : invalid (do_set) left-hand side to assignment
> FALSE <- 0L
Error in FALSE <- 0L : invalid (do_set) left-hand side to assignment
> T <- F # yikes, this would be hard to debug!

Personally, I sometimes use T and F when I use R interactively, but I never use them in production code or packages.

How do T & F work as abbreviations for TRUE & FALSE in R?

They are objects in the base namespace.

find("T")
[1] "package:base"
find("F")
[1] "package:base"

Technically, you don't overwrite them, you create objects in an environment that has higher precedence in the search path that mask these (most likely the global environment), but you can still access them directly.

T <- FALSE
T
[1] FALSE
base::T
[1] TRUE

T' & 'F' reads as True and False from database

sqlQuery function has as.is option, from the RODBC manual:

as.is which (if any) columns returned as character should be converted to another type? Allowed values are as for read.table. See
‘Details’.

Details

... if as.is is true for a column, it is returned as a character
vector. Otherwise (where detected) date, datetime and timestamp values
are converted to the "Date" or "POSIXct" class. ...

Is it bad to explicitly compare against boolean constants e.g. if (b == false) in Java?

It's not necessarily bad, it's just superfluous. Also, the actual variable name weights a lot. I would prefer for example if (userIsAllowedToLogin) over if (b) or even worse if (flag).

As to the performance concern, the compiler optimizes it away at any way.

As to the authoritative sources, I can't find something explicitly in the Java Code Conventions as originally written by Sun, but at least Checkstyle has a SimplifyBooleanExpression module which would warn about that.

Outputting booleans for Rails in Heroku gives t instead of true and f instead of false?

Maybe you have 't' stored in your database for a true value for current_user.has_seen_store_intro, possibly because it was added by a different database adapter that supports booleans being stored as 't' and 'f'. Let's assume that signed_in? returns true -- in which case:

1.9.3p194 :008 > a = true
=> true
1.9.3p194 :009 > b = 't'
=> "t"
1.9.3p194 :010 > a and b
=> "t"

That's in Heroku. In your dev environment your database adapter understand 't' and 'f' as boolean values, therefore it returns true.

Just a possibility.

read.table reads T as TRUE and F as FALSE, how to avoid?

If all your columns are characters then try this:

# replace text = . with your filename
read.csv(text="A,B,T,T", header=FALSE, stringsAsFactors=FALSE,
colClasses = c("character"))

Else, you'll have to pass the type of each column in colClasses as: colClasses = c("numeric", "numeric", "character", ...)

Is there a downside to returning 1 or 0 instead of true or false in JavaScript?

It is not safe in the sense that it will have equivalent behavior, since 1 !== true and 0 !== false. It won't crash the browser, though. Ultimately, it depends on how you define "safe" -- and this is subjective.

However, you can return !0 for true and !1 for false, which will have the same bevaior as returning true or false, respectively, but this is going to make your code significantly less readable.

My two cents: Don't do a minifier's job. Write for readability and maintainability, then use a proper minifier as part of your deployment process if you want to reduce the size of your code.

Boolean comparison between dataframe and vector wrong

A few options:

## make comp the same shape as the data
## (fine with this example, impractical if you have more rows)
df > rbind(comp, comp)
# R_shunt R_Bosch R_1 R_2 R_3 R_4 R_5 R_6 R_7 R_8
# 5 TRUE TRUE TRUE FALSE TRUE FALSE FALSE FALSE TRUE FALSE
# 6 TRUE TRUE TRUE FALSE TRUE FALSE FALSE FALSE FALSE FALSE

## transpose df so the order works, then transpose it back
## (note this results in a matrix)
t((t(df)) > comp)
# R_shunt R_Bosch R_1 R_2 R_3 R_4 R_5 R_6 R_7 R_8
# 5 TRUE TRUE TRUE FALSE TRUE FALSE FALSE FALSE TRUE FALSE
# 6 TRUE TRUE TRUE FALSE TRUE FALSE FALSE FALSE FALSE FALSE

## Use Map to iterate in parallel - first column of df vs first item of comp, ...
## (returns a list, so we convert to data frame)
as.data.frame(Map(">", df, comp))
# R_shunt R_Bosch R_1 R_2 R_3 R_4 R_5 R_6 R_7 R_8
# 1 TRUE TRUE TRUE FALSE TRUE FALSE FALSE FALSE TRUE FALSE
# 2 TRUE TRUE TRUE FALSE TRUE FALSE FALSE FALSE FALSE FALSE


Dealing with TRUE, FALSE, NA and NaN

To answer your questions in order:

1) The == operator does indeed not treat NA's as you would expect it to. A very useful function is this compareNA function from r-cookbook.com:

  compareNA <- function(v1,v2) {
# This function returns TRUE wherever elements are the same, including NA's,
# and false everywhere else.
same <- (v1 == v2) | (is.na(v1) & is.na(v2))
same[is.na(same)] <- FALSE
return(same)
}

2) NA stands for "Not available", and is not the same as the general NaN ("not a number"). NA is generally used for a default value for a number to stand in for missing data; NaN's are normally generated because a numerical issue (taking log of -1 or similar).

3) I'm not really sure what you mean by "logical things"--many different data types, including numeric vectors, can be used as input to logical operators. You might want to try reading the R logical operators page: http://stat.ethz.ch/R-manual/R-patched/library/base/html/Logic.html.

Hope this helps!



Related Topics



Leave a reply



Submit