Professional Documents
Culture Documents
X 1introduction To R:, 7-9, 7 5, 7/5, SQRT (7), 2 7, 1+5 6, (1-5) 6
X 1introduction To R:, 7-9, 7 5, 7/5, SQRT (7), 2 7, 1+5 6, (1-5) 6
X 1introduction To R:, 7-9, 7 5, 7/5, SQRT (7), 2 7, 1+5 6, (1-5) 6
After R is started, there is a console waiting for input. At the prompt (>), you can enter numbers
and perform calculations: 1 + 4, 4.1-3.9, 7-9, 7*5, 7/5, sqrt(7), 2^7, 1+5*6, (1-5)*6
Answer lines begin with [1].
We assign values to variables with the assignment operator "=" or "<-". The sentence x=1 at the
prompt will not give any answer for uxs, it just indicates to the program R that it has to store a
variable named x and to give to it the value 1. To make visible for us this assignment, we just
type the name x of the variable at a new prompt line, press ENTER and on the following line we
will see the answer [1] 1.
R functions are invoked by their name followed by a pair of parenthesis () containing zero, one
or more arguments. For example, c(-1, 2, 3.1) produces a vector with three numerical values.
All text after the pound sign "#" within the same line is considered a comment:
1 + 1 # this is a comment. 4*3
If you already typed a command and ran it by pressing ENTER and after that you decide to get
small modifications of that command, it is not necessary to retype it once again, you can directly
use the old version by pressing first the up key on yhelpour keyboard to see the history of all
commands executed until that moment and moving between them by up-down keys until you
find the command to be modified:
To move the cursor in some precise place of the old version of the command to be modified, you
use the left-right arrows keys. Type 198605+3, press ENTER, then modify by arrows this last
command to have 197605+3. First up arrow, then left 5 times, delete 8, write 7 and press
ENTER.
Sometimes we need additional functionality beyond those offered by the core R library. In order
to install an extension package, you should invoke function install.package(name of the needed
package). For example, after typing install.packages("gplots") at the prompt, you will see
indications about all operations ran by R to download and install automatically the package
gplots to your computer. This package contains functions needed for statistical plots (barplots).
To get help and additional documentation on a function, we can use ?function name or
help(function name). By typing ?c or help(c) at the prompt, we get documentation on the
function c in R. If you are not sure about the name of the function you are looking for, you can
perform a fuzzy search with the apropos function: apropos("nova")
gives you the list of all functions in R containing nova substring in their name.
Data in R can be of several types: numeric, integer, complex, logical, character. Real numbers
belong to numeric class in R. Type the following commands at the prompt on 4 lines and all
answers will be numeric:
A complex value in R is defined via the imaginary i: z = 1 - 5i; class(z) The command sqrt(-1)
gives the answer NaN=Not a Number and a warning message: In sqrt(−1) : NaNs produced.
To obtain the answer i to this square root, we have to convert the argument in complex number
and type sqrt(−1+0i) or sqrt(as.complex(−1)). z
Logical values are often created via comparison between numbers: L=5.5>6; L or L=pi<=4; L
The L after ; is used to display the value of the variable as answer. Command class(L) gives the
answer "logical"
Define u = TRUE and v = FALSE. Standard logical operations are &=and, |=or, !=negation:
u & v, u | v, !v, 1>3&7<=pi^2 Further details on these logical operations can be found by
typing help("&") and similarly for the other operations. We will learn in this way on the
functions xor(u,v) (equivalent of u|v), isTRUE(u), isFALSE(u), is.logical(u).
Characters are used to represent strings in R. We convert objects belonging to other classes into
strings by using function as.character: x = as.character(pi); x; class(x) Strings can be
concatenated by using paste function:
Sometimes, it is convenient to create a readable string with the sprintf function, in which %s
will be replaced with the second string argument and %d will be replaced with the third one (a
double/real number): sprintf("%s are %d RON", "Popescu", 100)
To extract a substring from a bihelgger one, we use the substr function. Here is an example
showing how to extract the substring between characters 3-12, including spaces: substr("Ana
are mere", start=3, stop=12)
To replace the first occurrence of the word "Ana" by "Maria" in a string, we use the
sub=substitute function with the syntax sub(old,new,big string):