Use of ~ (tilde) in R programming Language

The thing on the right of <- is a formula object. It is often used to denote a statistical model, where the thing on the left of the ~ is the response and the things on the right of the ~ are the explanatory variables. So in English you’d say something like “Species depends on Sepal Length, Sepal Width, Petal Length and Petal Width”. … Read more

do-while loop in R

Pretty self explanatory. Or something like that I would think. To get the effect of the do while loop, simply check for your condition at the end of the group of statements.

What is the difference between require() and library()?

In addition to the good advice already given, I would add this: It is probably best to avoid using require() unless you actually will be using the value it returns e.g in some error checking loop such as given by thierry. In most other cases it is better to use library(), because this will give an error message at … Read more

How to unload a package without restarting R

Try this (see ?detach for more details): It is possible to have multiple versions of a package loaded at once (for example, if you have a development version and a stable version in different libraries). To guarantee that all copies are detached, use this function. Usage is, for example or

Error in : target of assignment expands to non-language object

These errors occur when you try to assign a value to a variable that doesn’t exist, or that R can’t treat as a name. (A name is a variable type that holds a variable name.) To reproduce the errors, try: (Can you guess which of the three errors NULL <- 1 returns?) A little-known feature of R is that you can assign … Read more

What does “The following object is masked from ‘package:xxx'” mean?

The message means that both the packages have functions with the same names. In this particular case, the testthat and assertive packages contain five functions with the same name. When two functions have the same name, which one gets called? R will look through the search path to find functions, and will use the first one that it finds. In this … Read more