Introduction To R: Pavan Kumar A
Introduction To R: Pavan Kumar A
Pavan Kumar A
What is R?
There are several GUI editors of R language, out of which RGui and Rstudio
are commonly used.
Common characteristics of R
It can be used with other programming languages such as Python, Perl, Ruby,
Julia and on Hadoop & Spark
Basic Features of R - Packages
Matrix Arithmetic.
* is element wise multiplication
Assignment
To assign a value to a variable use “<-” or “=”
Getting Started
How to use help in R?
R has a very good built-in help system.
If you know which function you want help with simply use ?_______ with the
function in the blank.
Ex: ?hist.
>18+22/2-4/4*3.5
[1] 25.5
>(18+22/2-4/4)*3.5
[1] 98.875
HANDLING BASIC EXPRESSIONS
Mathematical Operators in R
+, -, *, () - Simple Mathematical operations
pi - Stands for Pie value
X^Y - X raised to Y
sqrt(x) - square root of x
abs(x) - Absolute value of x
factorial(x) - Factorial of x
log(x) - logarithm of x
cos(x), sin(x), tan(x) - Trigonometric functions
DECLARING VARIABLES IN R
Variables are symbols that are used to contain and store the values.
Two ways to assign the values
Numerical Vector
String/character vector
VARIABLE TYPES IN R
Numeric Vector:
Vector of numeric values.
A scalar number is the simplest numeric vector.
Example:
1.5
## [1] 1.5
To store it for future use,
X<-1.5
VARIABLE TYPES IN R - VECTORS
Constructing the numeric and character vectors in R
The numeric() is used to create a zero vector of given length
c(10,20,20,30,40)
It is a Numerical/Integer vector
is.numeric(c(1, 2, 3))
## [1] TRUE
is.numeric(c(TRUE, TRUE, FALSE))
## [1] FALSE
is.numeric(c("Hello", "World"))
## [1] FALSE
CONVERTING VECTORS strings <- c("1", "2", "3")
Different classes of vectors class(strings)
can be coerced to a specific ## [1] "character”
--------------------------
class of vector.
strings + 10
For example, some data are ## Error in strings + 10: non-numeric
string representation of argument to binary operator
numbers, such as 1 and 20. -------------------------
numbers <- as.numeric(strings)
We need to convert it to
numbers
numeric representation in ## [1] 1 2 3
order to apply numeric class(numbers)
functions. ## [1] "numeric”
----------------------------
numbers + 10
## [1] 11 12 13
CONVERTING VECTORS as.numeric(c("1", "2", "3", "a"))
Different classes of vectors ## Warning: NAs introduced by coercion
can be coerced to a specific ## [1] 1 2 3 NA
class of vector. -------------------------
For example, some data are as.logical(c(-1, 0, 1, 2))
string representation of ## [1] TRUE FALSE TRUE TRUE
numbers, such as 1 and 20. --------------------------
as.character(c(1, 2, 3))
We need to convert it to
## [1] "1" "2" "3"
numeric representation in
--------------------------
order to apply numeric
as.character(c(TRUE, FALSE))
functions.
## [1] "TRUE" "FALSE"
CALLING FUNCTIONS IN R
Many predefined functions are there in R.
To invoke, user has to type their names
For example
> sum(10,20,30)
1] 60
> rep("Hello",3)
[1] "Hello" "Hello" "Hello“
> sqrt(100)
[1] 10
> substr("example",2,4)
[1] "xam"
CREATING AND USING OBJECTS
R uses objects to store the results of a computation
> myobj<-25+12/2-16+(7*pi/2) Assigns a mathematical
> myobj Invokes the myobj object expression to an object
called myobj
[1] 25.99557
the R session.
When you access other files on your hard drive, you can use either
absolute paths (for example, D:\Workspaces\test-project\data\2015.csv)
In an R terminal, you can get the current working directory of the
running R session using getwd()
INSPECTING THE ENVIRONMENT
In R, every expression is evaluated within a specific environment.
An environment is a collection of symbols and their bindings.
If you type commands in the RStudio console, your commands are evaluated
in the Global Environment.
Example:
If we run x <- c(1, 2, 3), the numeric vector c(1, 2, 3) is bound to symbol x in
the global environment.
Global environment has one binding that maps x to integer vector
c(1,2,3)
HANDLING DATA IN R WORKSPACE
The ls() or objects() function is used to return the list of objects in the
workspace
> ls()
[1] "a” "b” "bubba" "fun" "levels" "msg“
[7] "myobj" "n" "x12" "yourname“
The rm() function is used to remove the variables that are not required
anymore in a session
> rm(a)
> ls()
[1] "b“ "bubba” "fun“ "levels” "msg” "myobj" "n"
[8] "x12" "yourname”
HANDLING DATA IN R WORKSPACE
getwd() function: Function used to display the current working directory of
the user
> getwd()
[1] "/home/bioinfo/pavank/rstudio-0.99.489/bin"
save() function: Function used to save the objects created in the active
session.
> save(x12, file="x12.rda")
It will save in the current working directory with the name “x12.rda”
You can also save entire working image save.image()
HANDLING DATA IN R WORKSPACE
load() function : Function used to retrieve the saved data
yourname<-"mary“
> ls()
[1] "b” "fun” "levels” "msg” "myobj” "n” "x12” "yourname"
> save(yourname, file="yourname.rda")
> rm(yourname)
> ls()
[1] "b” "fun” "levels" "msg” "myobj” "n” "x12“
> load("yourname.rda")
> ls()
[1] "b” "fun” "levels” “msg” "myobj” "n” "x12” "yourname”
Executing R Scripts
Creating and Executing R script on Windows:
Open Notepad, and write R commands
Save it has “filename.R”
From the Rgui, file->Open script. It opens a window for browsing the Rscript
Click Open
EXECUTING R SCRIPTS
Creating and Executing R script on Linux:
R script is the series of commands written and saved in .R extension
To run a script “/home/bioinfo/pavank/R/use1.R”