0% found this document useful (0 votes)
51 views11 pages

R Programming 2

The document outlines topics related to using R including: 1) The R environment and what R is. 2) Getting started with R and how to use the online help system. 3) Basic commands and execution in R including data types, data structures, and variable assignment. 4) Simple data manipulation in R including objects, vectors, generating sequences, and basic operators.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
51 views11 pages

R Programming 2

The document outlines topics related to using R including: 1) The R environment and what R is. 2) Getting started with R and how to use the online help system. 3) Basic commands and execution in R including data types, data structures, and variable assignment. 4) Simple data manipulation in R including objects, vectors, generating sequences, and basic operators.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 11

Outlines

§ Introduction

1.1. The R environment

1.2. Getting Stated with R and online help system

1.3. Commands and Execution

1.4. Simple manipulation

1.4.1. Objects; Vectors; Generating sequences

1.4.2. Data storage and Management

1.4.3. Arrays, Matrices, Lists, Data frames

1.4.4. Reading data

1.1. The R environment

What is R?

§ 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.

§ This programming language was named R, based on the first letter of the first name
of the two authors (Robert Gentleman and Ross Ihaka). And partly a play on the
name of Bell Labs Language S.

§ R is a platform for the object-oriented statistical programming language S.

§ R is free and easy to learn.


§ R runs on major platforms: Window, Mac Os, UNIX/Linus

The R environment

R is an integrated suite of software facilities for data manipulation, calculation and graphical
display. It includes

§ An effective data handling and storage facility.

§ A suite of operators for calculations on arrays, in particular matrices.

§ A large, coherent for calculation of intermediate tools for data analysis.

§ Graphical facility for data analysis and display.

§ A well-developed, simple and effective programming language which includes


conditionals, loops, user-defined recursive functions and input and output facilities.

1.2. Getting started with R and online help system

Starting R

§ The beauty of R is that it is a shareware, so it is free to anyone (no license fee) and
available online.

§ To obtain R from windows (or Mac). Go to the comprehensive R Archive Network


(CRAN) athttp://www.r-project.org orhttp://www.cran.r-project.org and you can
immediately install it

§ Once you have installed R, there will be an icon on your desktop. Double click it and R
will start up. When you do, a window should appear in your screen

§ The “>” is a prompt symbol displayed by R. This is R’s way of telling you that it’s ready
for you to type a command.

§ If a command is not complete at the end of a line, R will give a different prompt by
default “+” on the second and subsequent line and continue to read input until the
command is syntactically complete.

§ To see the list of installed datasets, use the data method with an empty argument:
>data().

Getting online help in R

§ R has a built-in help facility.

§ To get more information on any specific named function e.g. sqrt, mean e.t.c the
command is :

>help(sqrt) or

>?sqrt

§ For a feature specific by special characters, the argument must be enclosed in single or
double quotes e.g. (“[[“).

>help(“[[“)

§ This is also necessary for a few word with syntactic meaning, including if, for, while and
function.

>help(“for”)

1.3. Commands and Execution

Technically R is an expression language with a very simple syntax. Users are expected to type
inputs (commands) into R in the console window.

Commands

§ Consists of expressions or assignments

§ Are separated by semi-colon (;) or by a newline


§ Can be group together using braces (“{“ and “}”)

§ Expressions and commands in R are case-sensitive. e.g. X and x do not refer to the
same variable

§ Command lines do not need to be separated by any special character like semicolon in
SAS.

§ Anything following the hash character (#) R ignores as a comment

§ You can use the arrow keys on the keyboard to scroll back to previous commands.

The set of variables/symbols which can be used in R:

§ Variable name can be created by using letters, digits and the dot (.) symbol. The variable
name consists of letters, numbers and the dot or underlined character. E.g. Wt.male,
Var_name, Var, …

§ Variable name must not start with a digit or a dot (.) followed by a digit or vice versa.
E.g. 2var_name, 1var, .1x, 1.x … are not valid.

§ Avoid some special name use by the system, e.g. c, q, t, C, F, I,T, diff, df, pt – AVOID.

Basic Data Types in R

There are 5 basic data types in R

1. Numeric: The most common data type in R is numeric. A variable or a series will be
stored as numeric data if the values are numbers or if the values are decimals e.g c=3.7,
5, 6,1.231

2. Integer: Integer data are actually a special case of numeric data. E.g numbers of
children in a family … 1L, 3L, 6L (the L tells R to store this as an integer).

3. Complex: Complex numbers with real and imaginary parts. E.g 1+4i

4. Logical: A logical variable is a variable with only two values; TRUE or FALSE
5. Character: The data type character is used when sorting text, known as strings in R. The
simplest way to store data under the character format is by using ” ” around the piece of
text: e.g char = “male”.

· If you want to force any kind of data to be stored as character, you can do it by
using the command: as.character()

· Note that everything inside “ ” or ‘ ’ will be consider as character, no matter if it


looks like character or not. E.g. ‘7.5’ will be saved as character.

· Furthermore, as soon as there is at least one character value in a variable or


vector, the whole vector will be considered as character. E.g. char_name =
c(“text”, 1.237, 4), the whole vector will be considered as character.

Data structures in R

R has many data structures. These include:

1. Vector: A vector is the most common and basic data structure in R and is pretty much
the workhorse in R. e.g. c(3,4,5) or c(“male”, “female”).

2. List: A list is an R-object which can contain many different types of elements like vector,
functions and even another list inside it. e.g. list(c(1,2,3), 21.3, sin60, c(2,4,1))

3. Matrix: A matrix is a two-dimensional rectangular data set. It can be created using a


vector input to the matrix function.

4. Data Frames: Data frames are tabular data objects. Unlike matrix, in the data frame each
column can contain different modes of data. The first column could be numeric while
the second column could be character and the third column could be logical. It is a list
of equal length. Data frames are created using ”data.frame()” function.

5. Array: While matrices are confined to two dimensions, arrays can be of any number of
dimensions, the array takes a dim attribute which creates the required numbers of
dimension.
6. Factor: They are data objects which are used to categorize the data and store it as
levels. They can store both strings and integers. They are useful in data analysis for
statistical modeling. They are created using the ”factor()” function by taking a vector as
input.

Variable Assignment

The variable can be assigned values using leftward, rightward and equal to operator. The values
of the variable can be printed using “print()” or simply print the variable name.

# Assignment using equal operator. # The result of the above code

Var1 = c(1,2,3) [1] 1 2 3

# Assignment using leftward operator.

Var2 <- c("learn","R") [1] "learn" "R"

R Operators

OPERATOR NAME EXAMPLE OUTPUT

+ Addition >5+2 [1] 7

- Subtraction >5-2 [1] 3

* Multiplication >5*2 [1] 10

/ Division >5/2 [1]1.666667

^ Exponent >5^2> [1] 125

%% Modulus 5%%2 [1] 1

%/% Integer Division >5%/%2 [1] 2


== Equal >5==2 [1] FALSE

!= Not Equal >5!=2 [1] TRUE

>/< Greater/Less than >5>2 [1] TRUE

>= Greater than or equal to >5>=2 [1] TRUE

<= Less than or equal to >5<=2 [1] TRUE

: Create a series of no. >1:3 [1] 1 2 3

%*% Matrix Multiplication M1%*%M2 [1]

1.4 Simple Manipulation

Objects; Vectors; Generating Sequence

Objects

§ 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.

§ R saves any object you create.

§ To list the object you have created in a session use either of the following commands:
>object() or ls().

§ To remove all the object in R type: >rm(list=ls(all=T).

§ To remove a specified number of objects use: >rm(x,y), only object x and y will be
removed.

§ To quit the R program use the close(X) button in the window or you can use the
command q().

Vectors

§ Vectors are the simplest type of object in R and it is simply a list of items that are of the
same type.

§ They can be created with c then combined function

§ There are 3 main types of vectors in R.

1. Numeric vectors

2. Character vectors

3. Logical Vectors

1. Numeric Vectors: is a single entity consisting of an ordered collection of numbers.

E.g. To set up a number vector X consisting of 5 numbers, 10, 6, 3, 6, 22. We use any one of
the following commands:

>X = c(10, 6, 3, 6, 22) or #Output

>X<-c(10, 6, 3, 6, 22) or [1] 10 6 3 6 22

>assign(“X”,c(10, 6, 3, 6, 22))

2. Character Vectors: A character or strings are used for storing text. A string is
surrounded by either single quotation marks or double quotation marks, but is printed
using double quotes (or sometimes without quotes). “hello” is the same as ‘hello’. For
example, we create a vector variable called fruits;

>fruits = c(“banana”,”apple”,”orange”)

>fruits #Print Fruits

[1] “banana” ”apple” ”orange” #Output

3. Logical Vectors: A logical vector is vector whose elements are TRUE, FALSE, or NA.
TRUE and FALSE are often abbreviated as T and F respectively. Comparison operators
are <, >, <=, >=, ==, !=.

Logical Operators

§ &: Element-wise logical AND operator. It returns TRUE if both elements are TRUE.
§ |: Element-wise logical OR operator. It returns TRUE if one of the element is TRUE.

§ &&: Logical AND operator. It returns TRUE if both statements are TRUE.

§ ||: Logical OR operator. It returns TRUE if one of the statement is TRUE.

§ ! : Logical False. It returns FALSE if statement is TRUE.

NOTE: The logical operator && and || consider only the first element of the vectors and give a
vector of single element as output.

Example: Consider x=c(3,4,TRUE) and y=c(4,1,FALSE), the print

a) x&y

b) x|y

c) x&&y

d) x||y

>x=c(3,4,TRUE)

> y=c(4,1,FALSE) #Output

> x&y [1] TRUE TRUE FALSE

> x|y [1] TRUE TRUE TRUE

>x&&y [1] TRUE

>x||y [1] TRUE

Generating Sequence

R has a number of ways to generate sequences of numbers, includes:

§ Using operator with numeric data


>4:12

[1] 4 5 6 7 8 9 10 11 12

>10:1

[1] 10 9 8 7 6 5 4 3 2 1

>3.8:11.4

[1] 3.8 4.8 5.8 6.8 7.8 8.8 9.8 10.8

Note: The colon operator has high priority within an expression. e.g 2*1:10 is equivalence to
2*(1:10)

§ Using sequence (Seq.), seq() function

>seq(1:10), >seq(from=1, to=10), >seq(to=1,from=10) >seq(1,10)


# are all equivalence to 1:10

>seq(1,10,by=2) [1] 1 3 5 7 9

Matrices, Arrays, Lists and Data Frames

Matrices

§ In R, matrices are an extension of the numeric or character vectors with dimensions; the
numbers of row and columns.

§ As with vectors, all the elements of a matrix must be of the same data type.

§ A matrix is created using matrix() function.

§ The basic syntax for creating a matrix in R is :

matrix(data, nrow, ncol, byrow, dimnames)

Example:
>X=matrix(c(3,4,5,6,7,8,9,10,11,12,13,14),nrow=4,byrow=TRUE)
>X
>Y=matrix(c(3:14),4,3,byrow=T)
>Y
When we execute the above code, it produces the following result:

[,1] [,2] [,3]

[1,] 3 4 5 >> Try others yourself

[2,] 6 7 8

[3,] 9 10 11

[4,] 12 13 14

§ By default the matrix is filled by column. To fill the matrix by row specify byrow = T as
argument in the matrix function.

§ Matrix operations (multiplication , transpose e.tc) can easily be performed using a few
simple functions like:

dim #dimension of a matrix (nrow and ncol)


%*% #matrix multiplication
t() #matrix transpose
det() #determinant of a square matrix
solve() #matrix inverse; also solves system of linear equations
eigen() #compute eigen-values and eigenvector
diag(x) #create diagonal of a square matrix
ncol(A) #returns number of columns of matrix A
nrow(A) #returns number of rows of matrix A

You might also like