This week I was running computations transforming some input files into output files. The problem was that it was a repeated process. If new input files were generated or old ones were updated I needed to calculate new output files. The transformation was time consuming so I wanted to run the calculations only when required.
My initial code was:
It should skip files for which output file modification time is greater than input file modification time. However, I quickly learned that it fails when the output file is missing as file.info returns NA for missing files. The fix I have found was to use isTRUE function which returns TRUE only if its argument is exactly TRUE and otherwise will return FALSE.
The only shortcoming of isTRUE function is the fact that if its argument is TRUE but has some attributes it returns FALSE. This can be seen in the following code:
The conclusion is that the safe form of the loop in the above example should be using [[ indexation:
This brought me to the problem of vector subsetting using logical arguments. See the following example:
When vector y contains NA it will be returned as NA in output vector. Sometimes such behavior is desirable, but there are cases when it is not. For example notice that we do not know if NA generated by x[y] is because vector x contained NA and y was TRUE or simply y was NA.
However, as we can see from the example using sapply(y, isTRUE) solves the problem. Fortunately y attributes are dropped before passing values to isTRUE so we get correct result without removing them beforehand.