openlang

Freezer with zero readings reports status "ok", what am I missing

My nightly audit script (Rscript, base R) flags freezers whose peak temp is above a limit. Last night U-03's logger was unplugged, so every temp_c for it in the export is the string ERR. The report still says U-03 is ok. Nobody got paged. The printed table shows n_valid 0, peak_c -Inf and mean_c NaN for that unit. The other units look right.

I assumed the reading step was broken. I added "ERR" to na.strings, checked that temp_c comes in numeric, and ran with --verbose. The row count is correct. Nothing changed.

The relevant part:

readings <- read.csv(infile, stringsAsFactors = FALSE,
                     na.strings = c("", "NA", "ERR"))

peak_c = max(df$temp_c, na.rm = TRUE),
mean_c = round(mean(df$temp_c, na.rm = TRUE), 1)
...
res$status <- ifelse(res$peak_c > LIMIT_C, "ALARM", "ok")

LIMIT_C is -65. R also prints a warning near the end of the run. My terminal already complains about LC_CTYPE on every start, so I stopped reading warnings. Once, a job of mine ran for a month on a warning like that.

Is this the reading step or something after it?

The conversation continues 2

Follow the reasoning
  1. Permalink

    It's after the reading step. max() on an empty vector with na.rm = TRUE gives -Inf, and -Inf > -65 is FALSE, so you get "ok". That's the warning you stopped reading. Check n_valid first:

    res$status <- ifelse(res$n_valid == 0, "NO DATA",
                         ifelse(res$peak_c > LIMIT_C, "ALARM", "ok"))

    See ?max for the empty-input behavior.

Other sides of the same question

  1. Ported a short MATLAB script to R and one result came back as a matrix

    Hello all. I'm porting a short MATLAB script from a colleague (Priyesh's, from 2023) into R, because the rest of our pipeline…

    Replies 2Latest activity rmatlab
  2. A loop that allocated a column at a time

    Slicing in Julia copies, which I knew and then forgot inside a loop. Take a function that walks the columns of a matrix and d…

    Replies 2Latest activity julia
  3. Totals wrong only for day with one interval (readmatrix? Apple silicon?)

    Small MATLAB report for electricity meters, one CSV per day, columns = meters, rows = 30 min intervals. Run it with matlab -b…

    Replies 1Latest activity matlab