Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

R - Chapter 1

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

R Programming

UNIT I- Chapter 1

Introduction
R is a programming language and software environment for statistical analysis, graphics
representation and reporting. R was created by Ross Ihaka and Robert Gentleman at the
University of Auckland, New Zealand, and is currently developed by the R Development Core
Team.
The core of R is an interpreted computer language which allows branching and looping as well
as modular programming using functions. R allows integration with the procedures written in
the C, C++, .Net, Python or FORTRAN languages for efficiency.
R is freely available under the GNU General Public License, and pre-compiled binary versions
are provided for various operating systems like Linux, Windows and Mac.
R is free software distributed under a GNU-style copy left, and an official part of the GNU
project called GNU S.

Features of R
As stated earlier, R is a programming language and software environment for statistical
analysis, graphics representation and reporting. The following are the important features of R:
• R is a well-developed, simple and effective programming language which includes
conditionals, loops, user defined recursive functions and input and output facilities.
• R has an effective data handling and storage facility,
• R provides a suite of operators for calculations on arrays, lists, vectors and matrices.
• R provides a large, coherent and integrated collection of tools for data analysis.
• R provides graphical facilities for data analysis and display either directly at the
computer or printing at the papers.

R Console and Editor Panes


There are two main window types used for programming R code and viewing output. The
console or command-line interpreter that you’ve just seen is where all execution takes place
and where all textual and numeric output is provided. You may use the R console directly for
calculations or plotting.You would typically use the console directly only for short, one-line
commands.
By default, the R prompt that indicates R is ready and awaiting a command is a > symbol, after
which a text cursor appears. To avoid confusion with the mathematical symbol for “greater
than,” >, some authors (includingme) prefer to modify this. A typical choice is R>, which you
can set as follows:
> options(prompt="R> ")
R>
With the cursor placed at the prompt, you can use the keyboard uparrow (") and down arrow
(#) to scroll through any previously executed commands; this is useful when making small
tweaks to earlier commands.
For longer chunks of code and function authoring, it’s more convenient to first write your
commands in an editor and execute them in the console only when you’re done. There is a

V Semester BCA -R-Programming- Introduc�on & Vectors `1


built-in R code editor for this purpose. The R scripts you write in the code editor are essentially
just plain-text files with a .R extension.

Comments
In R, you can annotate your code with comments. Just preface the line with a hash mark (#),
and anything that comes thereafter will be ignored by the interpreter. For example, executing
the following in the console does nothing but return you to the prompt:
R> # This is a comment in R...

R script

R script is simply a text file containing (almost) the same commands that you would enter
on the command line of R.

R for Basic Math

All common arithmetic operations and mathematical functionality are ready to use at the
console prompt. You can perform addition, subtraction, multiplication, and division with the
symbols +, -, *, and /, respectively. You can create exponents (also referred to as powers or
indices) using ^, and you control the order of the calculations in a single command using
parentheses, ().

V Semester BCA -R-Programming- Introduc�on & Vectors `2


E-notations

Assigning Objects

In R, you can assign objects (variables) using the assignment operator <- or the equal sign =.
Both methods are commonly used for variable assignment. Here's how you can assign objects
in R:
R> x <- -5
R> x

V Semester BCA -R-Programming- Introduc�on & Vectors `3


[1] -5
R> x = x + 1 # this overwrites the previous value of x
R> x
[1] -4
R> mynumber = 45.2
R> y <- mynumber*x
R> y
[1] -180.8
R> ls()
[1] "mynumber" "x" "y"

Vectors
A vector in R is a fundamental data structure that is used to store a collection of elements of
the same data type. It is one of the most basic and versatile data structures in R, and it forms
the building block for many other data structures and operations in the language. Understanding
vectors is crucial for working effectively with R because many R operations are inherently
vectorized, allowing you to perform operations on entire vectors at once.
Here are the key characteristics and details of vectors in R:
Homogeneous Elements: All elements within a vector must be of the same data type. R has
several atomic data types, including:
• Numeric: This type includes both real and integer numbers.
• Character: Used for text data.
• Logical: Represents Boolean values (TRUE or FALSE).
• Integer: Specifically for integer values.
• Complex: For complex numbers.
The function for creating a vector is the single letter c, with the desired entries in parentheses
separated by commas.
• R> myvec <- c(1,3,1,42)
• R> myvec
• [1] 1 3 1 42

Sequences, Repetition, Sorting, and Lengths in R (Vector Functions)

In R, you can work with sequences, repetitions, sorting, and calculate lengths of objects to
perform various data manipulation and analysis tasks. Here's how you can perform these
operations:

Sequences: In R, you can create sequences of numbers using the seq() function or the colon :
operator. Sequences can be arithmetic, geometric, or custom.

Arithmetic Sequences:

You can create an arithmetic sequence using the seq() function. It takes arguments for the
starting value, ending value, and the increment.

# Creating an arithmetic sequence from 1 to 10 with an increment of 2


seq1 <- seq(from = 1, to = 10, by = 2)

V Semester BCA -R-Programming- Introduc�on & Vectors `4


# Creating the same arithmetic sequence using the colon operator
seq1 <- 1:10

You can repeat elements or sequences using the rep() function.

The rep function is given a single value or a vector of values as its argument x, as well as a
value for the arguments times and each. The value for times provides the number of times to
repeat x, and each provides the number of times to repeat each element of x. In the first line
directly above,you simply repeat a single value four times. The other examples first use rep
and times on a vector to repeat the entire vector, then use each to repeat each member of the
vector, and finally use both times and each to do both at once.
If neither times nor each is specified, R’s default is to treat the values of times and each as 1 so
that a call of rep(x=c(3,62,8.3)) will just return the originally supplied x with no changes. As
with seq, you can include the result of rep in a vector of the same data type, as shown in the
following example:
R> foo <- 4
R> c(3,8.3,rep(x=32,times=foo),seq(from=-2,to=1,length.out=foo+1))
[1] 3.00 8.30 32.00 32.00 32.00 32.00 -2.00 -1.25 -0.50 0.25 1.00

Sorting with sort


Sorting a vector in increasing or decreasing order of its elements is another simple operation
that crops up in everyday tasks. The conveniently named sort function does just that.

V Semester BCA -R-Programming- Introduc�on & Vectors `5


The sort function is pretty straightforward. You supply a vector to the function as the argument
x, and a second argument, decreasing, indicates the order in which you want to sort. This
argument takes a type of value you have not yet met: one of the all-important logical values. A
logical value can be only one of two specific, case-sensitive values: TRUE or FALSE.

Finding a Vector Length with length

Subsetting and Element Extraction

You can access individual elements of a vector using square brackets []. R uses 1-based
indexing, meaning the first element is accessed with [1], the second with [2], and so on.

my_vector <- c(10, 20, 30, 40, 50)


first_element <- my_vector[1] # Access the first element (10)
second_element <- my_vector[2] # Access the second element (20)

V Semester BCA -R-Programming- Introduc�on & Vectors `6


Using the square-bracket operator to extract or delete values from a vector does not change the
original vector you are subsetting unless you explicitly overwrite the vector with the subsetted
version.

suppose you want to piece myvec back together from qux and bar. You can call something like
this:

As you can see, this line uses c to reconstruct the vector in three parts: qux[-length(x=qux)],
the object bar defined earlier, and qux[length(x=qux)]. For clarity, let’s examine each part in
turn.

V Semester BCA -R-Programming- Introduc�on & Vectors `7


As with most operations in R, you are not restricted to doing things one by one. You can also
subset objects using vectors of indexes, rather than individual indexes. Using myvec again from
earlier, you get the following:

V Semester BCA -R-Programming- Introduc�on & Vectors `8


Vector-Oriented Behavior

Vectors are so useful because they allow R to carry out operations on multiple elements
simultaneously with speed and efficiency. This vectororiented, vectorized, or element-wise
behavior is a key feature of the language, one that you will briefly examine here through some
examples of rescaling measurements.

This code creates a sequence of six values between 5.5 and 0.5, in increments of 1. From this
vector, you subtract another vector containing 2, 4,6, 8, 10, and 12. What does this do? Well,
quite simply, R matches up the elements according to their respective positions and performs
the operation on each corresponding pair of elements. The resulting vector is obtained by
subtracting the first element of c(2,4,6,8,10,12) from the first element of foo (5.5 - 2 = 3:5),
then by subtracting the second element of c(2,4,6,8,10,12) from the second element of foo (4.5
- 4 = 0:5), and so on.

V Semester BCA -R-Programming- Introduc�on & Vectors `9


You can multiply two vectors as follows.

Here bar has been applied repeatedly throughout the length of foo until completion. Now let’s
see what happens when the vector lengths are not evenly divisible.

Lastly, as mentioned earlier, this vector-oriented behaviour applies in the same way to
overwriting multiple elements. Again using foo, examine the following:

V Semester BCA -R-Programming- Introduc�on & Vectors `10


You see four specific elements being overwritten by a vector of length 2, which is recycled

Consider the vector


foo,<-c(5.5 4.5 3.5 2.5 1.5 0.5)

__________

V Semester BCA -R-Programming- Introduc�on & Vectors `11

You might also like