Function writing

3 important questions on Function writing

How do you use the if() function in a function?

  • func<-function(x){ #start of body
    • if(specification){ #start of specification
  •     return(desired result)
      •   }#end of specification
    • else(second specification){ #start of specification
  •     return(desired result)
      • }#end of specification
  • } #end of function

How can you make custom warnings and errors in your function?

  • If(specification) {
    • stop("error message")
  • }
  • this will cause the function to stop and the error message to arise
  • If(specification) {
    • warning("warning message")
  • }
  • this will spawn a warning message, but the code will run on.

What does it mean to say that a function doesn't influence the global environment?

Using the undefined value x in your function and then asking for a result using function(1) does not alter x if x was earlier in your coding a pre existing value.

The question on the page originate from the summary of the following study material:

  • A unique study and practice tool
  • Never study anything twice again
  • Get the grades you hope for
  • 100% sure, 100% understanding
Remember faster, study better. Scientifically proven.
Trustpilot Logo