[R] approxfun asymmetry

Timothy Earl (Cefas) t|mothy@e@r| @end|ng |rom ce|@@@gov@uk
Fri Jul 4 12:50:26 CEST 2025


Hi,

I'm seeing an asymmetry in how approxfun treats -Inf and Inf, which I don't understand from reading the help file.

e.g. a simple step function
tmp <- approxfun(x=c(-Inf, -0.2, 0.2,  Inf), y=c(-1, -1, 1, 1))
tmp(c(-1, 0, 1))
# [1] NaN   0   1
I expected: # [1] -1   0   1

Clearly I can work round this by using type=2, and not specifying the end points (or by replacing -Inf by a large negative number), but I was wondering what I was missing about the different behaviour between positive and negative infinity.

To simplify further:
approxfun(x=c(-1000,  Inf), y=c(1, 1))(0)
# [1] 1

approxfun(x=c(-Inf,  1000), y=c(1, 1))(0)
# [1] NaN

Sessioninfo:
R version 4.3.0 (2023-04-21 ucrt) -- "Already Tomorrow"
Copyright (C) 2023 The R Foundation for Statistical Computing
Platform: x86_64-w64-mingw32/x64 (64-bit)

Thanks,

Tim
________________________________
 This email and any attachments are intended for the named recipient only. Its unauthorised use, distribution, disclosure, storage or copying is not permitted. If you have received it in error, please destroy all copies and notify the sender. In messages of a non-business nature, the views and opinions expressed are the author's own and do not necessarily reflect those of Cefas. Communications on Cefas’ computer systems may be monitored and/or recorded to secure the effective operation of the system and for other lawful purposes. All messages sent and received by the Centre for Environment, Fisheries and Aquaculture Science may be monitored in line with relevant UK legislation . <https://www.gov.uk/government/organisations/centre-for-environment-fisheries-and-aquaculture-science/about/personal-information-charter>


More information about the R-help mailing list