X 1introduction To R:, 7-9, 7 5, 7/5, SQRT (7), 2 7, 1+5 6, (1-5) 6

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

x=1Introduction to R

R can be downloaded from one of the mirror sites in http://cran.r-project.org/mirrors.html. You


should pick your nearest location.

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:

k=9; class(k) x=11.5; class(x) y=pi; class(y) z=exp(2); class(z)

The variable k, even if mathematically it is natural/integer, is considered to be implicitly real


(numeric). The fact that in R, the variable k is not considered integer can be tested by using the
function is.integer(k) having the logical answer FALSE. To create integer numbers in R can be
done by using the function k = as.integer(9). After this assignment, if we type class(k) the
answer will be integer. Or if we type is.integer(k) the answer will be TRUE. Function as.integer
rounds its argument toward 0: as.integer(pi), as.integer(2.9), as.integer(-2.9)

On the other hand, it is erroneous to apply this function on strings: as.integer("Joe")


yields the answer NA=Not Available and a warning message NAs introduced by coercion. On
the other hand, we are allowed to apply this function on logical arguments:
as.integer(TRUE) or as.integer(FALSE) giving the answers 1 and 0.

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:

nume = "Popescu"; prenume ="Maria"; paste(nume, prenume)

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):

sub("Ana", "Maria","Le-am rugat pe Ana si DiAna sa mearga pana la LiAna")

The gsub=globally substitute function is similar, but it replaces all occurrences:

gsub("Ana", "Maria","Le-am rugat pe Ana si DiAna sa mearga pana la LiAna")

LAB TEST FOR TODAY y=


• X=!
• Check the class of x and round it
• Introduce the logical variable y and z giving the truth value of the proposition x<1000,
respectively 𝑥 2 > 5000 and show their value. Compute (y and z) ,the negation of (y or z)
• |1-5i|, √7 − 4𝑖
• Construct the vector (1,8,-7.9) by using c function
• Define the variable string by concatenating the strings “Va veni” and “vacanta mare”
• Extract from string the characters 4-8
• Replace in string all a-s by Amare.

You might also like