Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 526895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:49:30+00:00 2026-05-13T08:49:30+00:00

the function I am writing specifies the behaviour of a physical switch: it should

  • 0

the function I am writing specifies the behaviour of a physical switch: it should be turned ON if a value goes above an upper threshold and can go again OFF if it goes under the lower threshold. a similar logic would describe a normal thermostat in a household oven. obviously I want it to work on vectors, that’s the whole point!

so if I have the data

S <- c(50, 100, 150, 180, 210, 200, 190, 182, 175, 185, 195, 205)

my function tells if the oven temperature is all right. the logical inverse of “switch the oven on”.

R> thresholdOnOff(S, 180, 200)
 [1] FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE  TRUE

the question is about programming style: I first tried to write it with an ‘apply’ function in it but I had forgotten to take environments into account… so I wrote a working version with a ‘for’ loop – which I didn’t like, then remembered about the environments and I’m not sure about the two versions:

thresholdOnOff <- local( {
  ## following the R inferno
  f <- function(series, lower, upper, initialValue=FALSE) {
    status <<- initialValue

    switchOnOff <- function(x) {
      if(x > upper)
        status <<- TRUE
      if(x < lower)
        status <<- FALSE
      return(status)
    }

    sapply(series, switchOnOff)
  }
} )


thresholdOnOff <- function(series, lower, upper, initialValue=FALSE) {
  ## just guessing and reading from the documentation
  status <- initialValue

  switchOnOff <- function(x) {
    if(x > upper)
      assign('status', TRUE, inherits=TRUE)
    if(x < lower)
      assign('status', FALSE, inherits=TRUE)
    return(status)
  }

  sapply(series, switchOnOff)
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T08:49:31+00:00Added an answer on May 13, 2026 at 8:49 am

    Here’s one efficient solution without loops:

    library("zoo")
    S <- c(50, 100, 150, 180, 210, 200, 190, 182, 175, 185, 195, 205)
    
    thresholdOnOff <- function(x, low, high, initial.value=FALSE) {
        require("zoo")
        s <- rep(NA, length(x))
        s[1] <- initial.value
        s[x > high] <- TRUE
        s[x < low] <- FALSE
        return(na.locf(s))
    }
    
    thresholdOnOff(S, 180, 200)
    

    Alternatively, you can use ifelse in one line to solve this, but that will be much slower if you have big data:

    na.locf(ifelse(S > 200, TRUE, ifelse(S < 180, FALSE, NA)))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing function that involve other function from base R with a lot
I'm writing a function, which determine the number of useful bits of a 16
I'm writing a function which will drop a table if it already exists. It
I am writing a function in Haskell that deals with numbers beyond the length
I am writing a function short getBits(short data, int p, int n) I have
I'm writing a function that replaces long hex coded color ( #334455 ) with
Short of writing a function manually that translates a few known REFIID to names,
I am writing a function to extract decimals from a number. Ignore the exception
I'm writing a function add_event as the following shows: function add_event(o, event_type, callback, capture){
Please help me writing a function which takes two arguments: a list of ints

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.