You are on page 1of 21

Introduction to R

Data types and functions


Learning objectives
• Basic use of R and R help
• How to give R commands
• R data structures
• Reading and writing data
• Some more R commands (exercises)
R project

• ”R is a free software environment for statistical computing and


graphics” (http://www.r-project.org)
• ”Bioconductor is a software project for the analysis of genomic data”
(http://www.bioconductor.org)
• Currently works as an expansion to R
R Data Structures

Linear Rectangular

All Same Type VECTORS MATRIX*

Mixed LIST DATA FRAME


R Data Structures

# vector group of single objects or elements


(all elements must be the same mode)

# factors vector of values that are limited to a fixed set of values (categories)

# list group of objects called components


(can be different modes and lengths)

# array group of vectors with more than 1 dimension


(all elements must be the same mode)
format: array(data=NA, dim=c(dim1, dim2, ..)

# matrix 2-dimensional array (group of vectors with rows and columns)


(all elements must be the same mode).
format: matrix(data=NA, nrow=1, ncol=1, byrow=FALSE)

# data frame 2-dimensional list (group of vectors of the same length)


(can be different modes)
format: data.frame(row, col)

Images from: https://www.stat.auckland.ac.nz/~paul/ItDT/HTML/node64.html 5


Vector
Factor
Lists
Two most common object types
for statistics:

matrix
data frame
Matrix
• a matrix is a vector with an additional attribute (dim) that defines the
number of columns and rows
• only one mode (numeric, character, complex, or logical) allowed
• can be created using matrix()
x<-matrix(data=0,nr=2,nc=2)
or
x<-matrix(0,2,2)
Matrix
Data Frame
• several modes allowed within a single data frame
• can be created using data.frame()
L<-LETTERS[1:4] #A B C D
x<-1:4 #1 2 3 4
data.frame(x,L) #create data frame
• attach() and detach()
• the database is attached to the R search path so that the database is searched by R when it is evaluating a variable.
• objects in the database can be accessed by simply giving their names
Dataframe
Data Elements
• select only one element
• x[2]
• select range of elements
• x[1:3]
• select all but one element
• x[-3]
• slicing: including only part of the object
• x[c(1,2,5)]
• select elements based on logical operator
• x(x>3)
Basic functions
Data input
Data input – Key board
Data input - files
Data input - Excel
Data output – File/Excel
In an R Session…

• First, read data from other sources


• Use packages, libraries, and functions
• Write functions wherever necessary
• Conduct Statistical Data Analysis
• Save outputs to files, write tables
• Save R workspace if necessary (exit prompt)

You might also like