Accessing df columns the wrong way causes empty vector, "no non-missing arguments to min/max, returning Inf/-Inf"

StackOverflow https://stackoverflow.com/questions/21691492

  •  09-10-2022
  •  | 
  •  

Question

I am a beginner on R, working on water quality data. PLease excuse my formatting mistakes. I am trying to run "nls" on my dataset. Running the script:

testingQModel<-nls(GR ~ GRm * (1-Kq/Q), data = testingQ, start = list(Kq = min(testingQ$Q), GRm = max(testingQ$GR)))

I get the following error:

Warning messages: 1: In min(x) : no non-missing arguments to min; returning Inf 2: In max(x) : no non-missing arguments to max; returning -Inf

The dataset does not have NAs and is all numeric. I ran range(testingQ, na.rm = TRUE) also with range(testingQ, na.rm = FALSE) just to give it a try either way and it returned maximum and minimum values in dataset all right. I am not sure what else to try. Look forward to a solution from someone! Thanks.

Was it helpful?

Solution

Summarizing what you already seem to have solved and written in the comments:

  • issue had nothing to do with nls or max
  • issue was due to a bad access to a dataframe column, either bad syntax or else you had shadowed the definitions of Q, GR by defining variables with those names
  • this then gave you an empty vector, which causes max to return Inf/-Inf
  • solution was to fix the column access, or don't define the offending shadowing variables

My tip: summary, max and min are a pain in R since they don't handle either NAs or completely empty inputs well. So always either eyeball their input vector to double-check for sanity, or else assign their input to a variable and inspect that.

OTHER TIPS

I agree with smci, summary function like min are painful in R.

There is a solution for this in the hablar package that solves that min/max returns Inf when given an empty vector. The function s converts an empty vector (NULL) to NA.

The problem

min(NULL)

[1] Inf
Warning message:
In min(NULL) : no non-missing arguments to min; returning Inf

Solution

library(hablar)

min(s(NULL))
[1] NA

disclaimer I am biased for this solution since I authored the package.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top