Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Alok Srivastava
CRRAO-AIMSCS, Hyderabad, INDIA
Jan 08, 2015
Getting Started with R
Alok Srivastava
Lecture - 02
Topics
Topics
1
How to use R
Data types in R
Data creation
Data curation
Getting
BasicsStarted
of R programming
Programming
with R
Alok Srivastava
Lecture
Lecture
101212
-02
02
Topics
Topics
1
How to use R
Data types in R
Data creation
Data curation
Getting
BasicsStarted
of R programming
Programming
with R
Alok Srivastava
Lecture
Lecture
101212
-02
02
Getting
BasicsStarted
of R programming
with R
Alok Srivastava
Lecture
Lecture
101212
-02
02
Linux
/home/alok/WorkShop/2014/Workshop_UoH_14_Jan/Lecture2
Windows
C:/Users/Alok/Documents
Alok Srivastava
Lecture
Lecture
101212
-02
02
Strings
Getting
BasicsStarted
of R programming
Programming
with R
Alok Srivastava
Lecture
Lecture
101212
-02
02
Getting
BasicsStarted
of R programming
Programming
with R
Alok Srivastava
Lecture
Lecture
101212
-02
02
Use R as Calculator
Airthmetic Operators
Addition
Subtraction
Multiplication
Division
Exponent
Modulus (x mod y)
^ OR **
Integer Division
x%/%y
H3
/
x%%y
H4
Variable
Getting
BasicsStarted
of R programming
Programming
with R
Alok Srivastava
H5
Mulitple Variable
Lecture
Lecture
101212
-02
02
Workspace in R
Save Workspace
Save workspace
save.image()
unlink(.RData)
save.image(mywork.Rdata)
load(mywork.Rdata)
savehistory(file=abc)
loadhistory(file=abc)
Quit Session
q()
Getting
BasicsStarted
of R programming
Programming
with R
Alok Srivastava
Lecture
Lecture
101212
-02
02
Character variable in R
- to store name or categorical variable
- Use double quotes to store variable
H6
Getting
BasicsStarted
of R programming
Programming
with R
Alok Srivastava
Lecture
Lecture
101212
-02
02
Getting help in R
Within R:
The ? Command can be used to get help on a specific command within R
? keyword or help(keyword)
# Command search
H6-b
library(help=pamr)
apropos("mean")
# list all functions containing
string mean
Search library functions
library(help=base)
# List of base function available with R console
library(help=samr)
# To display the list of function available in
package samr. But to display the help page, first we have to load the
library.
Documentation
Help files can be accessed in the text file or html format.
Manuals, reference cards, tutorials and news about recent developments
are available at http://www.r-project.org/other-docs.html
Online help
R-help : https://www.stat.math.ethz.ch/pipermail/r-help/
Bioconductor-help : https://stat.ethz.ch/mailman/listinfo/bioconductor
Getting
BasicsStarted
of R programming
Programming
with R
Alok Srivastava
Lecture
Lecture
101212
-02
02
PracticeSession:1
Getting
BasicsStarted
of R programming
Programming
with R
Alok Srivastava
Lecture
Lecture
101212
-02
02
Getting
BasicsStarted
of R programming
Programming
with R
Alok Srivastava
Lecture
Lecture
101212
-02
02
Variable types in R
Numeric
Integer
x=6
is.real(x)
is.integer(x)
# TRUE
# FALSE
Logical
x = c(1,2,3,4,5); y = (x<3);
Character String
x = c("M)
x = c("Kinjal","Madhav","Roopa","Suraj")
x = c("gene1","gene2","gene3","gene4",gene5)
Getting
BasicsStarted
of R programming
Programming
with R
Alok Srivastava
Lecture
Lecture
101212
-02
02
Vectors in R
Vectors may have mode logical,numeric,character.
Examples of Vectors
x = c(45, 90, 135 )
y = c("Kinjal","Madhav","Roopa","Suraj")
z = c(" gene1 " , " gene2 " , " gene3 " , " gene4" )
Getting
BasicsStarted
of R programming
Programming
with R
Alok Srivastava
Lecture
Lecture
101212
-02
02
Arrays in R
Vectors may also have mode logical,numeric,character.
Two dimension array is same as matrix
Examples of Two dimension array
x = array(data, dim)
x = array(1:3, c(2,4))
Getting
BasicsStarted
of R programming
Programming
with R
Alok Srivastava
Lecture
Lecture
101212
-02
02
Matrices in R
Col1
Col2
Col3
Col4
Row1
Row2
Row3
Is a matrix
Dimension : 3 X 4
Row size: 3
Column size: 4
Getting
BasicsStarted
of R programming
Programming
with R
Alok Srivastava
Lecture
Lecture
101212
-02
02
Data frames in R
Col1
Col2
Col3
Col4
Row1
Row2
Row3
All elements of any column must ,have the same datatype, i.e.
all numeric, or all factor, or all character, or all logical
Use for R modeling and graphical functions
If the data is read in using the command read.csv, read.txt etc, it will
automatically be saved as a data frame.
Getting
BasicsStarted
of R programming
Programming
with R
Alok Srivastava
Lecture
Lecture
101212
-02
02
Data lists in R
Row1
Row2
Row3
All elements of any rows must ,have the same datatype, i.e.
all numeric, or all factor, or all character, or all logical
Getting
BasicsStarted
of R programming
Programming
with R
Alok Srivastava
Lecture
Lecture
101212
-02
02
Getting
BasicsStarted
of R programming
Programming
with R
Alok Srivastava
Lecture
Lecture
101212
-02
02
Variable types in R
Numeric
Integer
x=6
is.real(x)
is.integer(x)
# TRUE
# FALSE
Logical
x = c(1,2,3,4,5); y = (x<3);
Character String
x = c("M)
x = c("Kinjal","Madhav","Roopa","Suraj")
x = c("gene1","gene2","gene3","gene4",gene5)
H7
Getting
BasicsStarted
of R programming
Programming
with R
Alok Srivastava
Lecture
Lecture
101212
-02
02
Vectors in R
Vectors may have mode logical,numeric,character.
Examples of Vectors
x = c(45, 90, 135 )
H8
y = c("Kinjal","Madhav","Roopa","Suraj")
z = c(" gene1 " , " gene2 " , " gene3 " , " gene4" )
Getting
BasicsStarted
of R programming
Programming
with R
Alok Srivastava
Lecture
Lecture
101212
-02
02
c(1,2,3,4)
from:to
rep(c(1,2,3,4),4)
array(1:3, c(2,4))
Getting
BasicsStarted
of R programming
Programming
with R
Alok Srivastava
H9
Lecture
Lecture
101212
-02
02
Arrays in R
Vectors may also have mode logical,numeric,character.
Two dimension array is same as matrix
Examples of Two dimension array
x = array(data, dim)
H10
x = array(1:3, c(2,4))
x = array(1:3, c(2,4,3))
Getting
BasicsStarted
of R programming
Programming
with R
Alok Srivastava
Lecture
Lecture
101212
-02
02
A = 1:3
B = 4:6
c = 7:9
# cbind combined object by Column
X = cbind(a,b,c)
H11
Getting
BasicsStarted
of R programming
Programming
with R
Alok Srivastava
Lecture
Lecture
101212
-02
02
go.term = c (GO0009117,GO0009253,GO0009354)
gene.count = c(15,18,25)
avg.expression.value = c(0.5432,0.2371,0.7867)
go.term.rank.rank= c(2,1,3)
H12
mydata = data.frame
(go.term,gene.count,avg.gene.expression,go.term.rank)
mydata2 =
data.frame(rank=1:4,gene_name=c("ddr1","apr2","bac","p53"),n=
c(.90,.75,.52,.31));
Getting
BasicsStarted
of R programming
Programming
with R
Alok Srivastava
Lecture
Lecture
101212
-02
02
genelist1 = c (abc1,abc2)
genelist2 = c(brca1,brca2,tp53,mdm2)
genelist3 = c(apr,erpn,myc)
mylist2 =
list(rank=1:4,gene_name=c("ddr1","apr2","bac","p53"),n=c(.90,.75,.52,.
31));
H13
Getting
BasicsStarted
of R programming
Programming
with R
Alok Srivastava
Lecture
Lecture
101212
-02
02
PracticeSession:2
Getting
BasicsStarted
of R programming
Programming
with R
Alok Srivastava
Lecture
Lecture
101212
-02
02
Getting
BasicsStarted
of R programming
Programming
with R
Alok Srivastava
Lecture
Lecture
101212
-02
02
Variable Information
is.na (x)
is.array(x)
is.vector(x)
is.matrix(x)
is.data.frame(x)
is.numeric(x)
is.complex(x)
is.character(x)
Getting
BasicsStarted
of R programming
Programming
with R
H14
Alok Srivastava
Lecture
Lecture
101212
-02
02
Variable conversion
as.vector(x)
as.matrix(x)
as.data.frame(x)
as.character(x)
Getting
BasicsStarted
of R programming
Programming
with R
H14
Alok Srivastava
Lecture
Lecture
101212
-02
02
Variable attributes
Attributes
length(x)
dim(x)
dimnames(x)
Getting
BasicsStarted
of R programming
Programming
with R
# Length of vector
# Dimension of matrix
# Dimension names
Alok Srivastava
H15
Lecture
Lecture
101212
-02
02
Missing Values
Variables of each data type (numeric, character, logical) can also take the
value NA: not available.
- NA is not the same as 0
- NA is not the same as
- NA is not the same as FALSE
H16
Alok Srivastava
Lecture
Lecture
101212
-02
02
H17
Getting
BasicsStarted
of R programming
Programming
with R
Alok Srivastava
Lecture
Lecture
101212
-02
02
Matrix curation :
x[r,c]
x[r,]
# row r
X[,c]
# column c
x[c(2,5,8)]
Matrix operation:
dim(x)
x+y
dim(x)
t(x)
diag(x)
nrow(x)
rownames(x)
rowSums(x)
rowMeans(x)
cor(x)
var(x)
Getting
BasicsStarted
of R programming
Programming
with R
H18
# Dimesnion of matrix
# Sum of matrix x and y
# Dimesnion of matrix
# Transpose of matrix
# Diagonal element of matix
# numer of rows
# row names
# row sum
# row means
# correlation matirx
# variance matrix
Alok Srivastava
Lecture
Lecture
101212
-02
02
Alok Srivastava
H19
Lecture
Lecture
101212
-02
02
sqrt(x)
sin(x), cos(x),
asin(x), acos(x)
log(x), log10(x), log(x,base)
exp(2)
max(x), min(x),
range(x)
sum(x)
mean(x)
median(x)
var(x)
sd(x)
Getting
BasicsStarted
of R programming
Programming
with R
Alok Srivastava
H20
Lecture
Lecture
101212
-02
02
abs(x)
ceiling(x)
floor(x)
trunc(x)
round(3.4578)
signif(3.4578)
Getting
BasicsStarted
of R programming
Programming
with R
# absolute values
# Ceiling
# floor
# trunc
# round, decimal place
# signif, significant digits
Alok Srivastava
H21
Lecture
Lecture
101212
-02
02
PracticeSession:3
Getting
BasicsStarted
of R programming
Programming
with R
Alok Srivastava
Lecture
Lecture
101212
-02
02
Exercise:1
Getting
BasicsStarted
of R programming
Programming
with R
Alok Srivastava
Lecture
Lecture
101212
-02
02
Alok Srivastava
Lecture
Lecture
101212
-02
02
Alok Srivastava
Lecture
Lecture
101212
-02
02
Getting
BasicsStarted
of R programming
Programming
with R
Alok Srivastava
Lecture
Lecture
101212
-02
02
Getting
BasicsStarted
of R programming
Programming
with R
Alok Srivastava
Lecture
Lecture
101212
-02
02
Dontforgetto
saveWorkspace..
Getting
BasicsStarted
of R programming
Programming
with R
Alok Srivastava
Lecture
Lecture
101212
-02
02
Alok Srivastava
Assistant Professor, CRRAO- AIMSCS, Hyderabad, INDIA
Date 8-01-15
Getting
BasicsStarted
of R programming
Programming
with R
Alok Srivastava
Lecture
Lecture
101212
-02
02