Technically R is an expression language with a very simple syntax. It is case sensitive as are most UNIX based packages, so A and a are different symbols and would refer to different variables. The set of symbols which can be used in R names depends on the operating system and country within which R is being run technically on the locale in use. Names are effectively unlimited in length. Elementary commands consist of either expressions or assignments.
If an expression is given as a command, it is evaluated, printed unless specifically made invisible , and the value is lost. An assignment also evaluates an expression and passes the value to a variable but the result is not automatically printed. This prompt may be changed by the user. We will generally omit the continuation prompt and indicate continuation by simple indenting.
Command lines entered at the console are limited 4 to about bytes not characters. The vertical arrow keys on the keyboard can be used to scroll forward and backward through a command history. Once a command is located in this way, the cursor can be moved within the command using the horizontal arrow keys, and characters can be removed with the DEL key or added with the other keys. More details are provided later: see The command-line editor.
The recall and editing capabilities under UNIX are highly customizable. You can find out how to do this by reading the manual entry for the readline library. If commands 5 are stored in an external file, say commands. R in the working directory work , they may be executed at any time in an R session with the command. For Windows Source is also available on the File menu.
- A Survey of Evolutionary Algorithms for Data Mining and Knowledge Discovery?
- Death of a Naturalist.
The function sink ,. The command. The entities that R creates and manipulates are known as objects. These may be variables, arrays of numbers, character strings, functions, or more general structures built from such components.
During an R session, objects are created and stored by name we discuss this process in the next section. The R command. The collection of objects currently stored is called the workspace. All objects created during an R session can be stored permanently in a file for use in future R sessions. At the end of each R session you are given the opportunity to save all the currently available objects. If you indicate that you want to do this, the objects are written to a file called. RData 6 in the current directory, and the command lines used in the session are saved to a file called.
When R is started at later time from the same directory it reloads the workspace from this file. At the same time the associated commands history is reloaded.
- The Bloody White Baron: The Extraordinary Story of the Russian Nobleman Who Became the Last Khan of Mongolia.
- Religious Motivation and the Origins of Buddhism: A Social-Psychological Exploration of the Origins of a World Religion (Routledgecurzon Critical Studies in Buddhism).
- Dead Silver: A Novel.
- Thought Experiments.
It is recommended that you should use separate working directories for analyses conducted with R. It is quite common for objects with names x and y to be created during an analysis. Names like this are often meaningful in the context of a single analysis, but it can be quite hard to decide what they might be when the several analyses have been conducted in the same directory.
R operates on named data structures. The simplest such structure is the numeric vector , which is a single entity consisting of an ordered collection of numbers. To set up a vector named x , say, consisting of five numbers, namely This is an assignment statement using the function c which in this context can take an arbitrary number of vector arguments and whose value is a vector got by concatenating its arguments end to end. Assignment can also be made using the function assign. An equivalent way of making the same assignment as above is with:.
Assignments can also be made in the other direction, using the obvious change in the assignment operator. So the same assignment could be made using. If an expression is used as a complete command, the value is printed and lost 8. So now if we were to use the command. Vectors can be used in arithmetic expressions, in which case the operations are performed element by element. Vectors occurring in the same expression need not all be of the same length. If they are not, the value of the expression is a vector with the same length as the longest vector which occurs in the expression.
Shorter vectors in the expression are recycled as often as need be perhaps fractionally until they match the length of the longest vector. In particular a constant is simply repeated. So with the above assignments the command. In addition all of the common arithmetic functions are available. If the argument to var is an n -by- p matrix the value is a p -by- p sample covariance matrix got by regarding the rows as independent p -variate sample vectors. Note that max and min select the largest and smallest values in their arguments, even if they are given several vectors.
The parallel maximum and minimum functions pmax and pmin return a vector of length equal to their longest argument that contains in each element the largest smallest element in that position in any of the input vectors.
Internally calculations are done as double precision real numbers, or double precision complex numbers if the input data are complex. R has a number of facilities for generating commonly used sequences of numbers. For example is the vector c 1, 2, …, 29, The function seq is a more general facility for generating sequences. It has five arguments, only some of which may be specified in any one call.
The first two arguments, if given, specify the beginning and end of the sequence, and if these are the only two arguments given the result is the same as the colon operator. That is seq 2,10 is the same vector as Arguments to seq , and to many other R functions, can also be given in named form, in which case the order in which they appear is irrelevant.
A related function is rep which can be used for replicating an object in various complicated ways. The simplest form is. Another useful version is.
As well as numerical vectors, R allows manipulation of logical quantities. The first two are often abbreviated as T and F , respectively. However there are situations where logical vectors and their coerced numeric counterparts are not equivalent, for example see the next subsection. In some cases the components of a vector may not be completely known. In general any operation on an NA becomes an NA.
The motivation for this rule is simply that if the specification of an operation is incomplete, the result cannot be known and hence is not available. The function is. Examples are. In summary, is. To differentiate these, is. Character quantities and character vectors are used frequently in R, for example as plot labels.
Where needed they are denoted by a sequence of characters delimited by the double quote character, e. Character strings are entered using either matching double " or single ' quotes, but are printed using double quotes or sometimes without quotes. Quotes for a full list. Character vectors may be concatenated into a vector by the c function; examples of their use will emerge frequently. The paste function takes an arbitrary number of arguments and concatenates them one by one into character strings. Any numbers given among the arguments are coerced into character strings in the evident way, that is, in the same way they would be if they were printed.
Note particularly that recycling of short lists takes place here too; thus c "X", "Y" is repeated 5 times to match the sequence Subsets of the elements of a vector may be selected by appending to the name of the vector an index vector in square brackets. More generally any expression that evaluates to a vector may have subsets of its elements similarly selected by appending an index vector in square brackets immediately after the expression.