You are on page 1of 22

F

A \ Rfor Beginners
V' . uch as SP SS (St ati sti cal Pa cka
R di ffers fro m other Wmd?w . le pro gra ms s d ti d
ge for th So cia l
. .
s sty and cli ck op tio ns for e
t~~ pre e me statistic~\ pro
.
Sclences ) tl1at uses menus with cho ose 'd tet·mi nal fac1hty wh ere all the wo rk is co
ces ses .
Instead. R has the R Co nso \e fea_tur ·\ t prov1 es a mp let ed
e t ia
. d st,,\e or tex t fun ctio
m com man ns. k e pra cti ce to lea rn. Th e use rs,
The R language, a\th ough not ve1 d. fficult ta es som
u ; •

wh o are
~ t a~or ) or oth er sim ila r too ls ,
familiar with SPSS. MATLAB mo stl y use R sof tw are
(M atn x Labor . y 'f na
package as a statist . . ~ xec nd s. It pro vid es a tro ub le- fre
~
l ca\ too\ ior easy e ut1.0n. o com l
t
r dea . 1 d
e and
. .
e\Tor-free enviro nm ent compar d c- lin g wi th co mp ex ata mm
e to o th ei. stat1st1cal too s io mg
concepts like Principal Compone
nt Analysis. .
R is an open source and free to d \ d pro gra mm ing lang uag e dis trib ute d un de r the GNU
own oa_ . Th e bas e sys tem an d pa ck ag
(GNU"s No t Unix) and GPL (Ge es are
nera\ Public L1c~nc~) ter ms bas
parts of R. Th e R Development e s ste m dis tri bu tio n of R.
Core Team ma mt am s the Th e
packages are authored and preser yF the off ici al ho me pa ge
ved by a large group of vol unt
of R project, the users can access eer s. rom . th R sys
the main source of data abo ut R. tem
·
itse lf add-on packaoes manuals doc All res ou rce s, 1.e.~ e
umentatio· ns,
' ::, ' ' etc., are access1'bl e fro m the off ici al ho me pa ge .

Check Your Understanding


1. , v hat are the different ap
plication areas of R language
Ans. R language can be used in any ?
field that requires a large am ou
analysis. Any research field, div nt of dat a sto rag e an d da ta
erse business fields like financ
bioscience, and so on, are some e, ma rke tin g, sup ply ch ain ,
application areas of the R lan gua
ge.
2. Who designed and developed
the R language?
Ans. Ross Ihaka and Robert Ge
ntleman designed the R langua
De velopment Core Team develo ge for sta tist ica l ana lys is. Th
ped the R lan
eR
. . C gua ge. Th ey pro vid ed dif fer ent
sources wn tte n m an d precom ·1 d b" .
fon ns h
p1
UN IX , Linux , Windows , and Ma e mary files for different op era tin g sys te sue as
h
cintosh.
ms sue as

No te R language is a free sof


tware package available unde;·G
integration with other procedure NU and-GPL]
s written in different languages
etc. like C, C+ ~ t;l :o Pe rm its
' Y hon , .N et,
-- -- -
1.2 INSTALLING R
Installation of R in any operating
system such as Windows , UNIX
similar software. Th e base system , etc., is as easy .
a~d add-on pac, kages a1e
, • . ,
the

_two maJor parts < asi nst au ·
collection of use r-contributed fun ofR . A ing Other
ctions, methods, documentation,
of a pac kag e focu ses on a specia
· l stat · · I · I d 1 and exa mp les. Th ?ac ka ge is
1st1ca me t io o ogy. e tun q ·
ion ati tya
GA pa ck ag e m. i y be in sta llc<
i by s up pl y ing it s na m e to th e fu nc tio n _i ns ~a
hu
ll.
st es t1mat1on o f co va na
.
nc e m atrices
f"1111 c tio11r1 I,ty fo r ro
nn e ct ion . ;\d d - 011 go od option
an a, .1i la bk In tc rll L'I co s ca se , us ing a sa nd wi ch pa ck ag e of R is a
s i:-- rcq 11 irc d . 111 thi c ro ss -se c tio na l, lo ng itu
di na l, or time-
, ia sJn<fo ich cs tin wt nr ty pe s of dn ta s uc h c1 s
S l<f f diffcrL' lll m at or (Z eil eis ,
th at c:-tirnatc~ Ilic CtT lff 1y pi n 1ll y us ed as a co va ria nc e ma tri x es ti
p:iL·b g c \-vhi c li is
:-c nc s . Th e s:rnd " ir h in g co m m an d .
n lo; idc d :111d in sta lle d v i;i lhc fo llo w
~()() . n _cm be dr rn

:1 ~ t L, 1 1 . 1--, l c k ,1 q P .s
" J •
( .s an ci w 1 r:
h ")
F,
I 1

ter att ac hi n g th e pa c ka ge us in g li br ar y
() fu nc tio n by
ali ty is a va il ab le af
Th e p,1(1'.1!-!C fi 1n cti on
g ive n be low.
(c1Jfl1 ,\ in g th e cn m nw nd

I F~ ' l J-, r r y ( " s c1 n d w i c h " )


1 Li
ck ag es can be
Ta b le I. I. An in clu siv e li st of av ai la bl e pa
ag es ar e gi ve n in
'\ a m es of so m e R pa ck
of fic ia l s ite.
dc nn 1l oa de d fro m th e
ages
TA BL E 1.1 Some R Language Pack
mg ev
rp art
Bo ot cla ss
J\:f atr ix base
M AS S da tas ets
1
Su n i\·al compJfor
I Co de too ls too ls
Cl us ter me tho ds
1 Parallel
Gr ap hic s
•··¥
St ats 4
tcl tk
Utils
, Nn et

di ng
C he ck Your U nd er st an
ides
1. W ha t is a pa ck ag
e?
pl es an d do cu m en ta tio n. R la ng ua ge prov
am
lle cti on of fu nc tio ns , ex e co nt ai ns ot he r packag
es
An s. A pa ck ag e is a co ns . Th e ba se pa ck ag
ul fo r m an y ap pl ica tio
di ffe re nt pa ck ag es us ef
d gr ap hi cs .
su ch as uti Js , sta ts, an
ge so ftw ar e?
tw o m aj or pa rt s of th e R la ng ua ge software .
2. W ha t ar e th e
es ar e th e tw o m aj or pa rts of th e R la ng ua
an d ad d- on pa ck ag se " an d ot he r ne ce ss ar
y functions.
An s. Th e ba se sy ste m po rta nt pa ck ag e " ba
s th e m os t im
Th e ba se sy ste m co nt ain
by to pi c?
us er br ow se di ffe re nt pa ck ag es th e users can
3. H ow ca n a
ov id es th e CR AN Ta sk Vi ew s fro m wh ic h
ite of C RA N pr
An s. Th e of fic ial we bs c a nd do wn lo ad th em as pe r th ei r
ne e ds .
es by to pi
ea sil y br ow se pa ck ag l website of
d in sta ll a pa ck ag e "x yz " fr om th e of fic ia
wn lo ad an
4. H ow ca n a use r do
nu na nd
CR AN ?
ck ag e "x yz '', th e us e r ca n us e th e co
~. Tu downloc:1d
an d in s tall a pa th e R la ng ua ge .
of
An
es ~x yz ") wh ich is a bu ill -in co m m an d
j ~ J-J . f.Jdr_· ~
Jr a~
__ __
Q Rfor Beginners
1.3.1 Simple Common Op
erators
Like oth er programming
languages , R also has sim
logic3\. etc. 11~ operators ple operators Such as ari
th .
operators.
can be used 2as described in Table 1. .
Also, Figure G::· ;elational,
? ains these
TABLE 1.2 Examples of operators with their outputs
Ol!erator ExamJ!le with 0!!,!PUt
# Subtraction > 4 - 3
(11 1
# Multiplication > 4 * 3
(11 12
# Division > 4 I 3
(11 1. 33 33 33
# Exponential > 4 A 3
(11 64

Rw (64·bil)
ta.-·r:oo Y- i!;,c t,d,g e -
·~
~ ~ il l! \
> 4- 3
[11 1
> 4/ 3
I [l) 1. 333 333
> 4•4
[ 11 16
> 4 ~3
, [l) 64
> 4\3
. ~r, or: une xpe cte
d inp ut in "41 3"

Figure 1.3 Arithmetic operations


1.3.2 Nesting of Operat
ors
R la ng ua ge allows th
Th fi 11 · e nesting
. e o owmg examp1e, wh of operators ' i.e. ' it allows operato tO b
ere the expression first su P1aced withi
wi th 2, de m on str ate s
nesting. btracts 3 fromrs4 ande then . _n operators.
multiplies the result

I > (4 - 3) * 2
[ 1] 2

1.3.3 Exit or Quit Command


Th e qu it co m m an d ex
its from the R Console
. The syntax of the quit
command • giv
is . en b l
I >q ()
e ow .
ij
A Short Introduction to R G
source Command
1. 3'4
source comm:md executes from the current working directory. If the command<; are stored in
,he ·ternnl file nn d R in .
. tI,c wor k.mg dm.:ctory .
work, then 1t. cn n he exec uted at any time .
in an
R
an e:x • .
.-·on using the 1ollowmg command .
se~~,
-- so urce (" comma nd s . R" )
I
thl..' command. " comma nd s . R" is the external file where commands arc stored. For Windows
111
op\.,,. 1.."..,1 1·11 :0--- ,system.
_ source is also available on the File menu.

1.3.5 Sink Command


The sink command restores the output to the given file or to the console. The following command
diyerts all subsequent outputs from the console to an external file, "record . 1 is ".

I >sink( "record.lis")

The followi ng sink command restores it to the console once again.

I >sink ()

Check Your Understanding


1. Briefly describe the working of R language.
Ans. R is an interpreted programming language and all commands are directly executed in console.
They do not require creating and executing a complete program like other programming
languages, such as C or C++.
2. What is meant by [ 1] and "> +" in R console?
Ans. In R console, every output is displayed with [ 1 ] , which indicates that every program code
calls every single number as a vector or a matrix. Whereas, " > +" works as a secondary
prompt that occurs when any instruction or command is not finished. In this case. either the
codes need to be completed or press "Esc" to get back on the primary prompt. i.e .. " >" .

1.4 SOME INFORMATION ON RCOMMANDS


Technically R is an appearance language with simple syntaxes. Given bt'low is some useful
information commonly related to the R commands.
• Risa case sensitive language as most packages are derived from the UNIX based packages.
For example, ' A' and ' a ' are different symbols and refer to diss imilar variables.
• R is similar to other languages when it comes to naming variables. The sd of symbols used
in R depends on the operating system and on the country in which R is being run. Almost
' O R for Beginners
.· ~
s,,,i,btJl 's nrc used
aII alpIUllllll 11 l , 11l .. .
in . variable names .as well as symbols, . ' a
. . l I' ·1111, l1 "s the rcsfricti on that rl should ' slarl with '.' .or a letter· an d thatrJd ' , .
' . : . fl
' 3fl3.., l n~ l U, ·'
• '

.·111 ' , the second


'
clwn-1e lcr mu sl nol be
'
a d1 g1 t. Length s of
.
r

names arc uni · . ,r it "


I n, I te . ·1l1
• • . , .. , . ,
\\ I d 1nR
• Fithcr 3 scm, -rnlon (;) or a new 1111c scpa1::rtcs the commands m R.
a single ·
• Th~ br:iccs ( { .. . } ) combine lhc different elementary comma nds into corn
Po1Jr
e\pressinn.
• In R console. the symbo l hash mark(#) is used for comme nts. The user can use the Corr,
fller
nm,,·here.
• R ~r ill gire n difterent prompt (by defa ult, operator +) on aJJ next lines until
the
does not complete at the end ofa line. However,.the user may change this prompt ucornniar
. d. .
. b . 1e mdentin . 11e/she
g. c,
omit the continuation prompt and 111 1cate contmuat1on y s1mp
• The command lines entered on the console are limited to about 4095 bytes.
• R pro,·ides a mechanism for recaJJing and re-executing previous commands. e venj .
Th
c
arrow keys 011 the keyboard scroll forward and backward through a command histo
locatino the command, the horizontal arrow keys move the cursor within the com mand ry. AfiE
. J
~ ·
addition, characters can be removed with the DEL key or other keys.

Check Your Understanding

1. Are variables 'H' and 'h' same in R language?


Ans. R is a case-sensitive language and hence, 'H' and 'h' both are different.
2. How can a user recall and re-execute the previous commands in R?
and re-
Ans. Using the vertical and horizontal arrow keys on the keyboard, the user can recall
execute the previous commands in R.
3. How many types of escape sequences are available in R language?
of escape
Ans. Like other programming languages, R language also provides different types
sequences. For example, it provides \n, \r, \t, \v, \b, \f, and \a escape
sequences.

1.5 SPECIAL VALUES


or missing
R language provides some values to represent special values such as infinite value
value.

1.5.1 NA
The special value. "NA" stands for "not available". These values are used to signify missing values
. . .. to text
. R he or she accesses a value that 1s not availab le. R loads it in L
m • The user sees NA when
the NVL
values to represent the missing values or loads data from the databases for replacing
Rfor Beginners . . _
. I . I , ,111der.\Cnn' r'/l((racter ,s useJ Jor ulloca1;
. IR 1md '" S-P "" · t I< . • I i on, h11
In som< ' ,1/ikr r <"n 10 11 ' 0 · . , . ( R / /c!l n', this 1s us4 11 to 1(110 W so that when , ' th1
. . . I hi , 11 /'Cc'C l1f releo' t ., o · . J ou fr
/, 17<1 //IJJf!.c' l" <Tl,, < ' / ' . • , ·Jll f i1'11 r if docs not wot k. } i,
' C ; , , , , , ,·( d (· 117 R 1'(HI (',111 , l ,(!.IJ/( I .
11' ( •, - . ' .

Check Your Understanding

l \\ hat do you ml'a n b~' obj ects in R lan gua ge? . .


· ·, . , . ,~ kilc1,,,11 as obi'ect-ori ented prog ramming language as 1t stores everythi r
\ n, R l:rn12.u:.H!L t~ a ~o . d. h d b ,
· · · in. thl?~ob1-~ects. 1n e, .e1.Y_ R s' ess ion, obiJ ects are created

and store m t e ata ase ( . P.da t- . -'.'.l

, h · t ,,·oi·ki·110 director)'· In R, fun ction s, li sts, vectors, etc., are th e exam ples of
t
() I f Cll!Tc?ll O

0l.,_iects.
., How can one assion value to the objects?
Ans. The yaJue to the o~ects of R is assigned using left arrow with a hyphen (<- ) or right arro1
with a hyphen (- >). Although the 'equal to' operator may also be used but it is not truste
as it generates e1rnrs during programming.

1. 7 FUNCTIONS
Functions are tb e base of any programming language. The user can get only so far by typing i
calculations. Most of the statistical analyses require a number of functions and R language provide
the required fun ctions. R is not so much a statistical package in the traditional sense as Mini tab is
but it is really a programming language designed to be good for carrying out statistical analyses.
It has a variety of short readymade pieces of code that can carry out tasks, like managing use
data. These readymade pieces of codes are called functions. Along with this, functions can perforn
complex mathematical operations, draw graphs, and carry out statistical analyses ranging.
ln other words, a fu nction is a group of statements used to perform a particular task. Ead
fu nction name ends in a pair of brackets and for many of the more straightfonvard functiom
us.er needs to just type in the name of the function and put the name of the object. Many built-ir
fu nctions are avai lable in R. Some examples of built-in functions are given in Table 1.3 along witr
lheir outpu ts. -

TABLE 1.3 Built-in functions and their outputs


Function
Example with ~utput
~•: alurn l l()g of J 5 > l og ( l5 )
---~--~-
[ 1 ] 2 . 7 0 805
b :p11rit flliaJ ur ' c ' ra ised to th e power 5 > e xp (5 )
f l] 14 8 . 41 32
Squan: f1J '; I ol (>4
> s qrr ( Ci<I )
I I ] il
Absolute (i .e.• um,ignc<lJ vu luc ol 5
, ,1b 0 ( -'..1 l
I I I '.1
A Shnrt lntroclu ction tn R

5011 le
kel'. 110i11ts related to .f1111L'tirms
• The user c:111 c11T~ nut cakulations 1ha1 arc more complex hy rrwking th e argument of 1he
1

runrt il111 phc bil bct\\'een the bra cket s) a calculation it self'. For ex ampl e. the fo ll owin g
Cl)111111 :ltld " ill return the sine of 15 plus th e va lu e or the object " ,rn ::w/'.~ r ".

I 'sin (l5+n ns we r )

• The usn l'an use brackets within the function 's brackets to make sure that co mpli cated
l'alculations are carried out in a correct order. The following command will return th e va lue
of ·e· rai sed to power the value ofx multiplied by 3, raised to power 1/3.

I >e xp ( (x*3 ) /\ ( 1/3 ))

• The user can use functions in creating new objects. The following command creates an object
called P that has a value of 1 divided by the square root of value of object y.

I > P< - 1 / sqrt (y)

1.7.1 Functions with Multiple Arguments


The functions discussed so far have a single argument between the brackets. In this section \Ve
v,·ill discuss functions with more than one argument. It is often possible to control, specific aspects
of the way the function operates by adding further arguments separated by commas. These extra
arguments serve to do things like modifying the given data.
In the example given below, the function round () rounds off a number to a certain limit of
decimal places. The user can specify the number to be rounded off by typing it in between the
brackets after the function and then specifying the number of decimal places to round to by adding
a second argument digits = with a comma separating it from the first argument. The following
set of commands explains the same.

>round (19 . 7564 , digits=2) # Rounding the value 17.43 25 t o t wo


place s after the decimal point.
[1] 19 . 76

>round (l7 . 4325 , digits=l) # Rounding the valu e 17 . 43 25 to one


place a ft e r the decimal point.
[l] 17 . 4

Most R functions have default values specified for most of their arguments, and if nothin~ is
specified, the function will just use the default value. If we do not define the number of di crits~for
r oun d (), it will assume that the number is rounded off to no decimal places. ~
I > round(1 6 . 77 84)
~ Rfor Beginn ers
5. In other words, it ca lcu lat es
.I [1) 17 •
r111,clfOl1,\
.
: 5 to base 2.
011
, , ,r:amJ'/cs .,.culatc s the, log
arith111 ° 11

Some mou c c:il


i ,
, ·0111t11n11tt) . d t get I 5.
l1c raise o
The fol\0" ·111~ l , ~ ha:, l
.

the n11111bcr th:it -·· ft ·


5 function.Veryo en rn
c base== 2 · ) · Jl
'lL"'c1l' ( lJ , . · if ()
. ·n the fun cti on ca .
•fied using si gn pos1t. 10
I [l
- , or;r,4 49
l - ·,· · • C\

1111ents are p
. le the ann at the arguments I
~ I
rec isely spec1 simply by their
11ean
nJ

In rhc' next e:u mp .


. ·ork out w 1
R. the fu nction can "
· dig its == 4 )
>s i9 ni f(P 1 1
[l ] 3 . 142

>s ig ni f(p i , dig its== 2 )


[l ] 3. 1

>s i gn if( pi , 4)
[l] 3 . 142

Check Your Understanding

1. What is a function? lan gu ag e,


of statements or commands for performing any task. In ev ery
Ans. A function is a group nctions ma y be eit he r us er- de
fin ed
enthesis () symbol. Fu
the function is represented by par
guage.
or built-in as provided by the lan
ction?
2. What is the use of sq rt( ) fun R that fi d ou t th e
(nu mb e r) is one of the bu ilt-in mathematical functions in In s
Ans. The sq rt rt ( 3 6) will ret urn 6.
. For example, sq
square root of the given number.
-

function?
3. What is the use of sig ni f ()
en value (first argument) to th 0
.
rou nds off the giv
Ans. The sig ni f () fun ctio n
le, the com ma nd s i . e oIVen nu mb er of
ent ). Fo r exa mp gn if( 2 3 , 1 ) ret ur ns
significant digits (second argum ·
2 as the output .
ction and ab s() function?
4. What is the use of ro un d( ) fun d
n function in R languag e tl 1~t
-.)
" 1< un s of f h giv .
Ans. The rou nd () functio.n is a. built-i . fun cti on re t e en v ·
. th e dec ima l place. Th e ab s ()
according to value giv en 111 tun1s the ab alu e
value of any given number. Solute

~
A Short Introduction to R GJ
SIMPLE MANIPULATIONS-NUMBERS AND VECTORS
1.8
I,. ·s q~ction. we will di scuss vectors and arithmeti c operati ons on vectors' log ical vectors, and
111 tll
, ., cter n ~ctors.
~ I1-1 1'1

1.8.1 Vectors
A rector is a fundamental data ty pe of R language. R operates on named data structures such as
;1t1tneric vectors. A vector may be defined as a single entity consisting of an ordered collection
of numbers. ln R, c () function is mostly used to create vectors. For example, to set up a vector
named p, consisting of four numbers, namely l 1.5, 6.8, 5.2, and 25. 8, the following R commands
are used. This is an assignment declaration using function c () and it produces the following output.

> p <- c(ll . 5 , 6 . 8 , 5 . 2 , 25.8 )

I >p
[l] 11 . 5 6.8 5 . 2 25 . 8

Similarly, assignments are also possible through the right side operator in R. Therefore, the same
assignment could be completed using the following command that produces the same result.

I >c (ll . 5 , 6 . 8 , 5. 2 , 25 . 8 ) - > p

In this context, c ( ).can take a random number of vector arguments. The value of c ( ) is a vector
obtained by concatenating its arguments end to end.
The assignment operator(<-) is placed strictly side-by-side to the object that receives the value
of the expression. In most situations, the '=' operator can be used as a substitute.
If an expression is used as an absolute command, the value is printed and lost. Therefore, the
following command is used such that the reciprocals of the four values are printed at the terminal
as shown (the value of p , of course, remains unaffected).

I > 1/p
[l] 0 . 0869 5 652 0.14705882 0.19 2 30769 0 .0 38 75 969

The following assignment command creates a vector y with 9 entries consisting of two copies of
p with a zero in the middle place.

I
> y <- c (p, 0 ' p)
> y
[ 1] 11. 5 6.8 5. 2 2 5. 8 0.0 11. 5 6.8 5.2 25 . 8

1.8.1.1 assign () Function


Assignment can also be done using the function a s s i g n () . A corresponding way of making the
same assignment as explained above is as follows:
R for Beg inn ers
6. 8, 5.2, 2'._i . 8 ))
L~ ( ll . 5 ,

I
~ as s_ i_g n ( " y" ,
" y
[l] 11 . 5 G. 8 5 . 2 25 . 8
0

1. 8.1. 2 F1111ctio11
ve ct ;o r () Th e fiun cti on tak es twon d
ec ifi ed len gth an d typ e. th
r of sp
The ve cto r () run crion
also creates a ve cto c, co mp lex , log ica l , etc
.) an d e se c
en t is the typ e (n um eri
arg um en ts.of which, the fir st arg um
e.
argum ent de fin es the siz
", 6 )
> ve ct or (" nu me r i c
[ l] 0 0 0 0 0 0
>
>
er i c ", 4)
> p <- v ec to r( "n um
> p
[l ] 0 0 0 0

1.8.2 Vector Arithmetic op er ati on s ar e ex ec ut ed


el em en t by
pr es sio ns wh er e th e
in ari thm eti c ex e len gt h.
Vectors may also be used isi ng in th e sa m e ex pr es sio n to be of sa m
ry for the ve cto rs ar gt h as th at of the
element. It is not ne ce ssa ll be th e ve cto r wi th th e sa m e len
of ex pr es sio n wi
If they are not, then the value ex pl ain s it.
ve cto r in the ex pr es sio n. Th e fo llo wi ng example
longe st

>p <- 4 .5

I > q< - 6 .2 5
>p+ q
[1 ] 10 . 75

The basic arithmeti


· ·
c operators are +,
as log ex
-,
p
*, /
sin
an d A _ In ad di tio n
b 1
cos tan ' sq rt ' etc . Gi ve n e ow ar e so m e e xa m p1es
·
to th es e , all th e re gu Iar an th m et ic
.

fu nct10ns are availab le, su ch ' ' ' ' n of th e


. . . .
fun ct1
.
0n s. Al so , Fi gu re 1.6 ex pla ins the ex ec ut io se co m m an ds in R
of bmlt-m anthmet1c
environment. nt s f
gest an d sm all es t ele me
• The ma x and mi n fun cti on s pic k the lar
n's value is a ve cto r of len gth tw o
'
na m ° ~ ve ct or respectively.
e1Y c (m in ( P) , m ax (p ) ) .
• The range of a functio ele me nts in p.
on gives the nu mb er of
• The le ng th ( p ) functi ele me nts in p.
gives the total nu mb er of
• The su m (p ) function the ele me nts in
calculates the pr od uc t of
• The pr od ( p ) function mp le mea , p.w h.1cl1
al fun cti on me a n (p ) ca lcu lat es the sa 'n is sa m e
• The statistic as su m ( )/
length(p) . P
( (p -m ea n (p )) 1\2 ) / (l e n gt h (
• The va r ( p ) function
giv es s um P) - l ) or th e
variance. sa mp le
a ve cto r of the sa me size as p with 1
revisi ts
• The s o rt (p ) function xib le so rti ng co mm an ds ava '_elaebm le
.e nt s Plac ed .
l"k
oth er mo re fle 1 1 an
n
inc rea sin g order. There are .
n for sor
.
t11 1g.
e or cJ.
t ()
uc e a pe rm uta tio E:.
so rt . li st () that pr od Or
U R for Beginners
Cit RGui (64-bit)
fit, [ d~ ~.,. Misc r•chges Window, l:ltlp

~li,-Jgl ~l•@J lil l!l

> 1: 20
20
[l] l 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
> 2*1:10
[l] 2 4 6 8 10 12 14 16 18 20
> 10 : 1
[1] 10 9 8 7 6 5 4 3 2 1
> I

Figure 1.7 Sequence using colon(:) operator

1.8.3.1 seq () Function ments


. . .. . s It has five argu
The seq () function 1s a more common fac1hty for generating sequence · ents, if given,
in which some are optional and can be used in any single call. The fir st two argum ments given,
indicate the beginning and ending of the sequence; and if these are the only tw~ ar~u e vector
15
then the result will be same as the colon operator. That is to say, seq ( 1, 2 0 ) t e sam
as 1:20.

I >seq(l , 20)
[l) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

Arguments can also be given in named form in which the order of their appearance is immaterial.
The first two arguments can be named as (fr om=va lue) and (to=va lue). Thus, seq ( 1, 1 O) :
seq ( f rom=l, to=l O) and seq (t o=l O, f rom=l) are all same as 1: 10.
Thenexttwoargumentstoseq() maybenamedas (by=value) and (length=value ) thal
specify the step size and the corresponding length of the sequence, respectively. If neither is given
then? The default value 1 is presumed. For example, the following command 0oenerates as
equenc(
from 2 to 3 using a step of .2.

I
>seq (2, 3 , by=.2) - > p
> p
[1] 2 .0 2.2 2 .4 2.6 2 . 8 3.0

Similarly, the following command also generates the same vector in pl and Fio
. . . . bl
1t m R environment as given e ow:
c Ure 1·8 eXp 1ain:

I
> pl<- seq(length=6, from=2, by=.2)
> pl
[1] 2 . 0 2 . 2 2 .4 2 .6 2.8 3 .0
A Short Introduction to R fJ)
ul (64-bit)
fdil 'fr"" fllisc f• ck,g,s Window, li•lp

-.■ oil~

> seq (2, 3, by= . 2 ) -> p


._ p
[ l] 2.0 2. 2 2.4 2.6 2.8 3.0
> pl<- seq( l e ngth= 6 , fro m=2 , by= . 2 )
> pl
[l] 2 .0 2.2 2.4 2 .6 2.8 3.0
> I
, .

Figure 1.8 Sequence function

The fifth argument can be named along=vector and is usually used as the only argument to
create the sequence 1, 2, ... , length (vector), or an empty series if the vector is empty.

J.8.3.2 rep () Function


Arelated function rep ( J is used for replicating an object in various complicated ways. The simplest
fonn of the command follows where three copies of p are placed end-to-end in p2.

> p2 <- rep(p, times=3)

I > p2
[1) 2 .0 2.2 2.4 2.6 2.8 3.0 2.0 2.2 2.4 2.6 2.8 3.0 2. 0 2 . 2 2 . 4
2 .6 2.8 3.0

Another useful version of the command is given below in which each element of p is repeated
three times before moving on to the next.

> p3 <- rep(p, each=3)

I > p3
[1 ] 2.0 2.0 2.0 2.2 2.2 2.2 2.4 2.4 2.4 2.6 2.6 2.6 2 . 8 2 . 8 2 . 8
3 .0 3.0 3.0

1.8.4 Logical Vectors


R also allows operations of logical quantities. TRUE, FALSE, and NA, are some logical values
generated by the elements of a logical vector. The relational operators are <, <=, > , >=, ==
(for accurate equality) and ! = (for inequality), and also logical operators used in logical vectors.
The following example, where x and y are two vectors, explains the concept of logical vectors.
After comparing both the vectors, the logical values are returned.

> x< - c (l,2, 3 )


> y< - c (S, 6 , 3 )
> x==y
[1] FALSE FALSE TRUE

I
.
G) R for Beginners
are indi cate d by
ter Vector . I beJs ; whe re requ ired , t~ey
used in R as plot a mpl e "H ell o " is a char acte r .stri ng.
1.8.5 Charac I
0111 mon Y
.
tes For exabl (" ), or smg le
(')
quo tes. U s mg the
. bl e quo ·
. ·ter vectors ate c ~ d by dou I . dou e
Cl131a1..: . d tine . ram or con sole . Cha ract er vect ors
ts e d ing eith er. mat
uence of charactepene e 1111g
.· ·e trate us . rint ed in a pro g
a seq ithout quotes, it ,s p .
Characte r stt mgs a1 .
funct10n.
double quotes, or son~ ettn :e:: tor using the c ()
can be concatenated mto
d con cate nate s them one by one
() Function gs
pas te . . ndo m num ber of argu men t~ an . t med into cha ract er strin
J.8.5.1 nts give n 1s u
f t'on1 obtams a ta
nbe r amo ng the argu me d f. It div ided in the resu lt by
The pa ste () unc _ . t d The argu men ts are by e au
into the character stnngs. An~ nut
in an obvious way in which tt was prm e .
a single blank character. s into a cha ract er vect or.
The follo wing set of comman d tum P
= \\If)

> p < - pas te( c ("A "),1 :5, sep


> p "AS "
[l] "Al " "A2 "
"A3 " "A4 "

>i< - 10 If
i)
i
> p aste ("T he val ue o f
1 O"
[l] "Th e v alu e of i =

Ch eck Your Un der sta nd ing

1. Wh at is a vect or? ing


ilar or diss imil ar data elem ents . In a pro gra mm
Ans . A vector is a data structure that stores sim s
uage , vect or play s a maj or role as it can stor e any type of data . R lang uag e also fea ture
lang
and the user can crea te a vect or usin g the buil t-in func tion "c () " or "ve ct O r () ,, .
"vector"
on func tion that con cate nate s the val ues in
In R language , the c () function is a con cate nati
a list, or in other words, creates a vector.
e?
2. Wh at is the use of len gth () in R lang uag .
Ans . In R language, the len gth () function
finds out the num ber of elem ents
pre sen t in the giv en
vector.
3. Wh at is a cha ract er vect or?
Ans A character vector is also a vect or that contains cha ract er ele 111·. en t s. The d b (" ) or sing le
- ·I . ract . ou le
• valu es m the cha
(') quotes are used to define t 1e character

----
e1 vec tor.

-- -- -- -
A Short fntrocluct ion tn R

1. g MATRICES AND ARRAYS


A 11 ,:itri\ is :1 two di nw11 siunnl nrrn y nl'11u111hcrs. 111 I{. the matri x t onccr t i" cx r c1 ndcd l11 ck rncnh
of .iny t\'pl' . for cxilmph:. a mntri x ur cliaraclcr strings. Matri ces an d arra y-; ,m: ch,1rac tcri 1cd a..,
, l:'l'l l1r~ " ·ith dimrnsio ns. The l11lh) wi11g sd ol' commands can create a matrix.

'- :-,_ , - 1:9


' di m(x ) <- C (J I j )
' X

[ , 1] [ , 2] l, 3J
[1, l 1 4 7
[ ::'. I l 2 5 8
[3 , l 3 6 9

1.9.1 dim () Function


The dim assignment function sets the dimension attribute of the matrix. In the exampl e gi ven
aboYe. dimension attribute of x causes R to take care of a vector of nine numbers as a 3 x 3 matrix.

1.9.2 matrix () Function


A suitable way to create matrices is to exercise the matrix function. The n r ow argument of
the function defines the number of rows. The byrow argument causes the matrix to be filled in
row-wise rather than columnwise.

> matrix (l: 9,nr ow=3 , byrow= T)


[,l] [,2] [,3]
[1 , l 1 2 3
[2 , l 4 5 6
(3 , l 7. 8 9

Few examples of the matrix function are explained in Figure 1.9 given below.
4t kwi6o4-h,I)
' r- t• ► »-< fKbte ...... ~
~l!Iii2lij]l!J

> ma tr ix(l : 9 , nro..,..3,byrow-T)


I, 1 I l. 2 1 I, 31
11, l 1 2 3
[2, l 4 5 6
[3, l 7 8 9
> matrix{l0 : 90,nrow-3,by row-T)
[,1 1 [,2 ] [,3] 1,4 ] [ ,51 1,6] 1,7] [,8 ] [ ,9 1 [ , 101 1,111 [,1 2 1 l , l ~ I l , l< I 1, 151
11,110 11 12 13 14 15 16 17 18 19 20 Jl ~~ ~3 24
12 , I 37 38 39 40 41 42 43 44 45 46 47 48 49 so 51
13, I 64 65 66 67 68 69 7 0 71 72 73 H ·15 ·1~ 71 76
[, 161 1,1 7) [,181 1,191 1,20) 1, 2 11 1,22) 1,23 1 1, 241 1, 2 > 1 l, Zb l t .J"I I
11,I 25 26 27 28 29 JO 31 32 33 H 3!> J6
12 ,J 52 53 54 55 56 57 58 59 60 61 ~~ -~
13 , J 79 80 81 82 83 84 85 86 e·1 Bil o• •Ill
> tnlltri :.iC(l0 :5 ,n r ov-J, b yro w-'J.' )
I, lJ I , 2 I
11, J 10 9
12 , J
ll,J
,I

Figure 1.9 Matrix examples


r
Q R for Beginn ers

193 t ra ns po si tio n () Function


· · T fi TR UE ),
· 1 o Lh e upp ercase_ or
Th e lrn nsr .· , - ru11 c 1101 L () ( no ti ce lh c low erc
ase t as o p pos ed L
.
tur ns . . o ~, ,on exa mp l e exp la in s thi s fun cti o n
. JO\ \~ 11110 l ' l))u mn s
and v ice ver sa . T he fo ll ow ing

r o w= T )
_· :--: . - m,i tri x ( l : 9 , nr o w'-=3 , by
.
1 ' h 1 ' ,c; n i' :· ( x ) "-- - LET TER S [ ]_ : 3 J
·, .~
i, 1J [ , 2] [, 3]
j 2 3
p '~ 5 6
C 7 8 9

t= Tr an spo se of a ma tri x is :

> p <- t(x )


> p
[, l ] [ , 2] [, 3J
[l , J 1 4 7
[2 , ] 2 5 8
[3 , ] 3 6 9

1.9.4 cb in d and rb in d Functions


um nw ise am
tor s tog eth er usi ng the cb ind and rb ind fun ctio ns , col
A use r can atta ch vec var iab le tha t enc los es th
ly. Th e cha rac ter vec tor LET TER S is an int egr ate d
ro w -w ise , res p ect ive
fol low s.
of both the functions is giv en as
cap ita l lett ers A-Z . An exa mp le
:1 2 )
> cb ind (P =l : 4 , Q=5 : 8 , R=9
p Q R
[l , J l 5 9
[ 2 , J 2 6 10
[3 , ] 3 71 1
[4, ] 4 8 12

)
>r bin d (P= l : 4 , Q=5 : 8 , R=9 :l2
[ , l] [, 2 ] [, 3 ] [,4 ]
P 1 2 3 4 Acc. No .... ...J.g J .3 3 ...
Q
P
5
9 10
6
11
7
12
8

Date .... ..... H/ J


~ ?:.?:::..
1.9.5 Matrix Multiplication
lic ati on, bo~h th
mu ltiplication, t~e ' * ' ope rat or is used. Fo r per for mi ng mu ltip
F~r ~a~ri_x mple sho ws the mu ltip lic ati on of ma
tnc es
be of sam e size . The follow ing exa
ma tn ces should
and x .
A Short Introduction to R E)
--.. X

[ , 1) l , 2 l I, 3 l
')
A l L. 3
B 4 5 6
C 7 8 9

'> p
[ , 1l [, 2] [, 3 ]
[1' ] 1 4 7
[2 , l 2 5 8
[ 3, l 3 6 9

> p*x
[ , 1 ] [ , 2] [, 3 l
[1 , l 1 8 21
[2 ' ] 8 25 48
[3, l 21 48 81

1.9.6 array () Function


An array is nothing but a collection of similar data elements. In R, arrays are also available. The
arra y (da ta vec tor, dim vector) creates an array in R. In this command, the first argument
contains the values and the second argument contains the dimension of the array. The user can also
define the names of rows or columns in.an array function. The following example creates an array
of size 2 x 2 that contains values from I :20.

>ar <- array(l:20,dim = c(2,2)


+ )
>a r
[,1] [,2]
[1 , l 1 3
[2 , ] 2 4

>ar <- array(l:20,dim = c(2),dimnames li st(c( " Ageu, " Names ")))
>a r
Age Names
1 2

Check Your Understanding


1. What is a matrix?
Ans. A matrix is a two-dimensional array of numbers. It is a vector and can be either one.
or two, or multidimensional. An array is a group of similar data elements and is also ~ailed
a one-dimensional matrix. The R language provides function s for creating matrices and
arrays which are mostly required during statistica l computation. The di m() function of the
R language sets the dimension attribute of the matrix.
R for Beginners ftJi l C ion
, of tne thi rd

An s.
\\' h at is ,1 m at rix
Tl . .
.
() fun cti
.
on '!
.

tri x. Th e firs t a rgu


me nt
an
dtn e
1C nv1 t1_ i;,. ;() "fl 111 L 1011 nl R lc1 11g uag c c rea tes a ma
·1· of roW 5 '
d L1,- ..
lllt: 'S the ran n L... L,r ' •1 ues. th e scc o ncJ arg um en t de
I fin es th e nu mb I
er
mn w1
.
se-
.:::--
· wi se rat h er tha n co u
ill"£!.lll11 L'l1t Ccl
~ '
US , ._ 1
, 1.. -
ti
e m a tn x to be fill ed in row of a rnatri X by
n fun cti on '?
J. \Y ha t is a tra ns po s itio the tran spo se
sit ion fun cti on t () of th e R lang uage ge nerate s
An s. Th e _tra ns po
an d co lumn s int o row s.
tur nin g row s into co lumn s

dif feren,
1.10 FACTORS ta,
In sta tis tic al sdatum
.
. . . lue s. . las or stage
~ . . va lue s int o sm all er va 1a 1 c '. .
A fac tor is ba sic all y the d1 v1s 1011 of .large . . . n of data, sue h as .soc nu me nc co de , su d
1sw
. . . les are us ed to spe cif y som e subd1v · pu t us mg a
an ab
ca teg ori ca·l ' . tan ne r sta ge of pu be rty, etc. Usually, these are m
· . ary dia gn os is,
pr im . ·de a fou r-l ev el fac tor
co ns istJ
fac tor s in R. ter ve cto r of len gth t
Ya ria ble s are ind ica ted as lev els for co mp act ne ss. On th e ms i h,
ara c
r d (b) a c tha t ex pla ins the co nc ep
Th e ex pre ssi on facto r has fou een 1 an
d 4
, _an
- . r of int eg ers be tw 1
ot two_ ite ms : (a) a ve cto the four lev els . Given below is an ex am p e
en clo sm g str ing s cla rify ing
of a factor.
, 1, 2)
>u n iq ue < - c ( 0 , 4 ,1
ni qu e, le ve ls= 0: 3) ", "l ar ge ")
> fu ni qu e< - fa ct or (u ne
d'
,,, " mo r e" , "m e iu m
~ <- c( "n o
> le ve l s( fu ni qu e )

>£ un iq u e mo re me diu m
[l] no ne <NA> mo re
me diu m la rg e
Le v e ls : -no ne mo re
r "u ni qu e"
the fir st co mm an d lin e ge ne rat es a nu me ric ve cto
In the ex am ple giv en ab
ov e, ca l va ria ble , a fac tor fu ni qu
E
tre at thi s as a ca teg ori
of five values. To tc
en co din g the un iqu e levels tor . Th is is ca lle d wi th on e arg um en t in ad dit ion
the function fac thin
is cre ate d from it by using tha t the inp ut co din g ex erc ise s the va lue s 0- 3. In
: 3 that specif y ine d i1
un iqu e, na me ly lev els =O r ind ica ted ch ara cte r str ing s. Th es e are ex pla
nged to the fou
lin e, the lev el na me s are cha
fig ur e J. JO giv en below.
t4lf f' Gu1 ({A L,tJ
- tJdp
t .. £de 'f,o,~ Id<>< /,<i"9 "'

··~
t~Ltt 1111t1t1o1■ 101 /j] [!]

;, U~l qUE <-


,. fur11 qu& ,. __ f oct 0r(
,.
c (Ci , 4, l , l , 2 )
uni gu t: ,
J•-·1•0J' "(L u1, iq ,1 t, )
l e vel a - 0: 3 )
<- c ("n one ", "mo re" ,
"me di um" , "la rge ")
,, ..,,.i ,-tur,
[1] Ci 4 l J. 2
..,,. fV rJJ.q u <:
mor e mor e me diu m
[ l } non e <UA;,
me dium Jnr ge
Le vel.'.l : n 0ne raor rs
> I
I.
Figure 1.1 0 Factors
A Sho rt In trod uction to R e
1.10 .1 as . num eri c () Function

1 hl' 1111
• 1 • \ H)11
L
,i ' • 11 I 1Jll' I 11 ,,,
I ',,._
fa ctorc; int<) num herc., In the fol\o\,\, mg
() U)l1 V t:rt <;, e"<a mpl e.
.
• ,
11 1
, 1~ cn n, c1 tcd mto ,1 num eric al coclm!!
r:-il' IIW . ·
a~ num er" h " \ 4 r hc n rJCT in:Jl inpu t cod ing
. (,f num hT~ 0 J Im!-- rli ~"PPC An~cl f rom th F-
111 \ Lfll)' - e f ,n 1 ' !' !' foct or.

-- . =i. . . .1l· (f uniq 11P )


111..1m c 1

I -,
i1l 1 NA ~: ?
lc-\' c l ~ (fu niq u o )
l 1 l " n ~') n e "
3

"m o re "

,'\'ore R also perm its the gen eration of an


pf unu sua l kind of fa ctor in wh, c h the ievd
- orde red It is pos sihl e usin g th e orde red
Jun ctio n con tain ing a s imil ar fact or Thn
1,otential~v help ful_as_rhey dJf[_er~'.!!iate nom in~l and ordi nal vari ab les from eoc h
i; an:.:
t: or1t
r>th u

Ch eck Your Un der sta nd ing


t. " 'hat do you und erst and by a fact or
in R lang uag e?
Ans . In R lang uag e, a fact or is noth ing
but a vec tor obje ct that con tain s a gro up
other vec tors of sam e leng th. It can be eith of e lem e nt.5 );·
er orde red or uno rde red. The :: =-::-:. -:: =-
creates a fact or arid the lev els () func tion func ti on
find s out the leYel of a fact or.
2. What is the use of fun ctio n as. num eric
()?
Ans. The func tion as . num eric ()
con vert s the fact ors into num ber s . Dur
com puta tion or any othe r calc ulat ion, it bec ing stat 1st1~a l
ome s nec essa ry to con vcn the fa~tor into
for com plex calc ulat ions . In this case , this num be r
func tion play s a maj or rol e b: con \ crti
factors into num bers . ng the

1.11 LISTS
A list is a coll ection of valu es . It is som etim
es valu able to mer ge a \.:ol kct ion of l_)bic-~l::;
co mbined obje ct. Thi s bec ome s poss ible in into ..1 \m -.t:-r
R lang uag e usin g lists . The use r cm- buil
it.., elements with the li st func tion . d ..1 fo,t t~m
A.., an exa mpl e. con side r a set of data plac ed
in two vc~tors A nnd B . To n\cr'=!~ th~~~ mJ.i
vectors into a li st, the user may use the list \ iJu,ll
fun~tion ns foll ows : -
.,, A <' - r, ( 7 g OO , ., 0 <,30 , 2 G8 0 , 1
) l '/ l) , I. ) 3 0 \) I
,1 8 / ~) , 1:.,:' \' ~ , ; "-' t I'
.,, I~ r ( ', <J CJ U , '/'/.. ·/ () , IJ B HO , ~' _, q \.) , 1 ..... , C' .., ~ , - ~
<' -
d~ •I q ~
"'
... • - ' •
.... I
'
, .\ \_) I \J . ~' l t,,,. I:.' ,1,, .... , ., l ' , ·'. ., ~ ~ • \
,., ·1· ,.J t,d ] ✓- I i :; I (li 1· ! 1> 1 1· /\ , dl l, •t l\}
..- TcJt 11 J
R for BeRinners

I
< •

1r "r, ;' hH 0 ')[70 6300 ~87S Ci508 7010 6535 6250 6790
• 4. '-

'i. ; 1) 4 FWO )2 1)0 58 49 464 0 516 0 6995 7595 6005 533 1


1

Thl' l'lrml'n h nf :i li~t arc named according to the argu~ent_names _utilis~d in the list. N
L'lcnw111, arl' 1.' >.tractcd usi ng the fo llow ing command which 1s explarned m Figure I.I a~~
1 giver
hdc,w :

I . ... ...
,.
:, · •:: ~: tefore
· ~· ~JOO 7090 26 80 5170 6300 4875 65 08 7010 6535 6250 679Q

~ .. i.... 11--P-<br-lll"d-<t,,i,

~~ti~

> A< - c:7~00, 7090, 2680,5170 , 6300 ,


• 5000 l
> B <- c(S9 90, 7270 ,4 880 , 5290,5849)
> Tc tell:~t <- lt ~t(before• A, a f te r =B)
> 7 v tall1~~
Sbe!cre
[1] 7900 7090 2680 5170 6300 5000

SaftE:r
[ 1 J 5s,90 7270 4280 5290 5849

> Totelli~t Si:>efore


[1] 7900 7090 2680 5170 63 00 5000
> To tell1~ ~$after
[ J.jJ 5990 7270 4880 5290 584 9
) .
Figure 1.11 Listfunction

Note Marry of R :S· built-in functions calculate more than a single vector of values and return ;
their results in the form of a list. '

Check Your Understanding


1. How can a user access or see a particular component of a list?
Ans. A list is al ways numbered and indexing starts from one. Using listn arne $cornponentname
or li s tn a.rne [ [ i ndex numbe r] ] , the user can access a particular component of the list.

1.12 DATA FRAMES


Data fra mes is th e most appropriate concept for representing tabular data. For scientific
ex periments, many a times, individual examinations are conducted, each of which engages
numero us measurements. In most cases, the dimensions of these measurements are dissimilar,
and at times, they represent the qualitative aspect and not quantitative. For business purposes,
A Short Introduct ion to R Q
dnl il is kept in databa se tabl es, each ol' which has many rows and columns represe nting different
~ntities. These dat a arc also kept in multiple formats.
qL'l , . .
A data lrame 1s a natural way to represent these datase ts m R. It represe nts a table of data. The
con1n1and da t a . frame (arg um e nt s ) is used to create data frame s in R. Eac h column may be
ora different type, but each row in the data frame must have the same length . For exampl e, the
following command will generate an error as arguments in vector ' a ' and ' b ' impl y different
number of rows. The vector ' a ' has 6 and ' b ' has 5 rows.

>data . fra me ( a =c ( 1, 2 , 3 , 4 , 5 , 6 ) , b=c ( 1, 2 , 3 , 4, 5) )

I Error in data . fra me (a = c (l, 2 , 3 , 4, 5 , 6 ) , b


3 , 4 , 5 )) :
c (l , 2 ,

Generally, the columns are named, and at times, the rows too are named. In a data frame , the
columns are called variables. Given below is a simple example of a data frame showing the top
mobile companies. Along with this, data frames are applied as lists with a class data . frame . The
explanation of these commands in R environment is given in Figure 1.12.

>C ompanies<- data.frame(


+Mobiles= c("Samsung","Apple","LG","Micromac") ,
+ra nk= c(l,2,3,4))
> Companies
Mobiles rank
1 Samsung 1
2 Apple 2
3 LG 3
4 Micromac 4

>typeof(Companies)
[l] "list"

> class(Companies)
[l] "data.frame"

Note The same processes may be used to refer to the items in lists and data frames. For
example, to extract the rank column from this data frame, the expression Companies $ra nk
can be used. The further chapters will discuss lists and data frames in detail.

Check Your Understanding


1. What is a data frame in R language?
Ans. In R language, a data frame is the natural way to represent a list with class " data . frame" .
A data frame represents a table of data. The type of ( ) function defines the type of the data
frame.
R for Beginners
-lt RGul (64 -bit)
fil• fdit l{iow Misc fack•gl!'J l'[indoW1 l:i•lp
1@t:1 liil! ~-101Ii]~
• Rconsole

> Companie s <- data.frame (


+ Mobiles= c (" Samsu ng","Apple","LG","Microm ac"),
+rank= c (l ,2 , 3, 4))
>
> Companies
Mobiles rank
1 Sams ung 1
2 Apple 2
3 LG 3
4 Micromac 4
>
>
> typeof(companies)
[1] "list"
>
>
> class(Companie s)
[1] "data.frame"
>I

Figure 1.12 Data frames

SUMMARY
• R is a programming language specially • The base system and a collection of
developed for statistical data analysis user contributed add-on packages are
and graphical representation . the two important parts of R system for
• R is a/so a software package that statistical computing.
contains different tools. These tools • A package is a collection of functions,
provide different types of facilities for examples, and documentation. The
data manipulation, data calculation, and functionality of a. package is focused
data storages along with the graphics on a special statistical methodology.
display facilities. • The official website of CRAN
• R language can be used in any field that (Comprehensive R Archive Network)
requires a large amount of data storage provides 8696 packages for different
and data analysis. Diverse business purposes.
fields like finance, marketing, supply • R is an interpreted programming
I
erous
chain, bioscience, etc ., use R language. language and supports nu rn
A Short In t roduc t ion t o R e
f unctio ns and co mm and s. Du e t o t h is, • M atrices and arrays are c haracte rised as
it is also ca ll ed fu nc t ion and c omm and - vecto r s with dimensions in R lan gu ag e .
line based pr o gra mming langu age. Th e d irn ( ) f un ctio n sets the d imen si o n
• R language is c as e se nsitive as are the att rib ute o f a m at rix .
majority of UNIX -b ased p ackages . For • In R lan g ua ge , a facto r is no th ing
example , ' A ' and ' a ' are different but a vector obj ect t h at conta ins th e
symb o ls and both would refer to group of elements of othe r v ectors of
di ss imilar variables . same length . It can be eith e r ordered
• R langua ge provides some values such or unordered . The factor () fu n ct io n
as NA , Inf , and - Inf for representing creates a fac t or .
so m e special values such as infinite • In R language, list is also an object t hat
v alue o r missing value . contains ordered collecti o n of obj ects
• A v ector is a data structure that stores or components. The compo n ents may
similar or dissimilar data elements. be strings, numbers , log ica l v a lu e s ,
complex numbers, or matrices .
• In R language , c ( ) function is
a concatenation function that • A data frame is a natura l w ay to
concatenates the values in a list, or in represent these datasets in R. A dat a
other words, creates a vector. frame represents a table of data .

KEV TER M S

✓ R Language R is a software package or elements that can be either numbers ,


programming language for data analysis or strings, or Boolean values, etc .
and graphical representation. ✓ Matrix A matrix is a two dimensio n al

✓ Command A command is a statement array of numbers.


that instructs the interpreter to execute ✓ Array An array is a group of sim ila r dat a

a given task. elements.


✓ Factor A factor is nothing but a v ec t o r
✓ Variable Variables are used to store
in any object that contains a group of element s
different type of values
of other vectors of same length .
programming language.
✓ List A list is an object of R conta in in g
✓ Object An object is any real world
the ordered collection of d ifferent type
entity. of objects.
✓ Function A function is a group of ✓ Data Frame A data frame is a natura l
statements for performing any task. way of representing the different ty pes
✓ Vector A vector is a data structure of data . A data frame represents a tabl e
that stores similar or dissimilar data of data.

You might also like