Chapter20Function Flashcards
how to get help with if forinstance in R
?’if’
You should not use | and & in IF statement
- You can use||(or) and&&(and) to combine multiple logical expressions.
4. You should never use|or&in anifstatement: these are vectorised operations that apply to multiple values (that’s why you use them infilter()). If you do have a logical vector, you can useany()orall()to collapse it to a single value.
usage of = , isidentical () in R. u need to be careful , why?
- Be careful when testing for equality.==is vectorised, which means that it’s easy to get more than one output. Either check the length is already 1, collapse withall()orany(), or use the non-vectorisedidentical().identical()is very strict: it always returns either a singleTRUEor a singleFALSE, and doesn’t coerce types. This means that you need to be careful when comparing integers and doubles:
if else , if else , what is the implication of have long chain of if else and what is the solution?
But if you end up with a very long series of chainedifstatements, you should consider rewriting. One useful technique is theswitch()function. It allows you to evaluate selected code based on position or name.
Coding style of of using {} with if and function
Bothifandfunctionshould (almost) always be followed by squiggly brackets ({}), and the contents should be indented by two spaces.
n opening curly brace should never go on its own line and should always be followed by a new line. A closing curly brace should always go on its own line, unless it’s followed byelse. Always indent the code inside curly braces.
example
if (y < 20) {
x
Function argument data ? how many types?
The arguments to a function typically fall into two broad sets: one set supplies thedatato compute on, and the other supplies arguments that control thedetailsof the computation
Generally, data arguments should come first. Detail arguments should go on the end, and usually should have default values. You specify a default value in the same way you call a function with a named argument
The default value should almost always be the most common value. The few exceptions to this rule are to do with safety. For example, it makes sense forna.rmto default toFALSEbecause missing values are important. Even thoughna.rm = TRUEis what you usually put in your code, it’s a bad idea to silently ignore missing values by default.
When you call a function, you typically omit the names of the data arguments, because they are used so commonly. If you override the default value of a detail argument, you should use the full name: # Good mean(1:10, na.rm = TRUE) # Bad mean(x = 1:10, , FALSE) mean(, TRUE, x = c(1:10, NA))
How to put space in function call ?
Notice that when you call a function, you should place a space around=in function calls, and always put a space after a comma, not before (just like in regular English). Using whitespace makes it easier to skim the function for the important components.
Choosing names
Generally you should prefer longer, more descriptive names, but there are a handful of very common, very short names. It’s worth memorising these
§ x,y,z: vectors.
§ w: a vector of weights.
§ df: a data frame.
§ i,j: numeric indices (typically rows and columns).
§ n: length, or number of rows.
§ p: number of columns.
How to check values and stop if not correct?
It’s good practice to check important preconditions, and throw an error (withstop()), if they are not true:
wt_mean
- Dot-dot-dot (…)
I do not understand
- Lazy Evaluation
Arguments in R are lazily evaluated: they’re not computed until they’re needed. That means if they’re never used, they’re never called. This is an important property of R as a programming language, but is generally not important when you’re writing your own functions for data analysis. You can read more about lazy evaluation
Enviroment in R
The environment of a function controls how R finds the value associated with a name.
For example, take this function:
f [1] 110
This behaviour seems like a recipe for bugs, and indeed you should avoid creating functions like this deliberately
Writing a function has three big advantages over using copy-and-paste:???
You can give a function an evocative name that makes your code easier to understand.
As requirements change, you only need to update code in one place, instead of many.
You eliminate the chance of making incidental mistakes when you copy and paste (i.e.updating a variable name in one place, but not in another).
Another advantage of functions is that if our requirements change, we only need to make the change in one place
Good code style is like correct punctuation.
When should you write a function?
You should consider writing a function whenever you’ve copied and pasted a block of code more than twice (i.e.you now have three copies of the same code).
Whare are the three key steps to creating a new function:
Pick a name
You list the inputs, orarguments, to the function insidefunction
You place the code you have developed inbodyof the function, a{block that immediately followsfunction(…).
It’s easier to start with working code and turn it into a function; it’s harder to create a function and then try to make it work.
It is good to check ur function wth diff input