SlideShare a Scribd company logo
Basic of R language
Name:
Roll No.
Learning aims
• 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
Packages
• R consists of a core and packages.
• Packages contain functions that are not
available in the core.
• For example, Bioconductor code is
distributed as several dozen of packages
for R.
– Software packages
– Metadata (annotation) packages
Starting the work with R
Start help
Help - Search engine
Help - packages
Anatomy of a help file 1/2
Function {package}
General description
Command and it’s
argument
Detailed description
of arguments
Anatomy of a help file 2/2
Description of how
function actually
works
What function
returns
Related functions
Examples, can be
run from R by:
example(mas5)
Functions or commands in R 1/3
• To use a function in a package, the
package needs to be loaded in memory.
• Command for this is library( ), for example:
library(affy)
• There are three parts in a command:
– the command
– brackets
– Arguments inside brackets (these are not always
present)
Functions or commands in R 2/3
• R is case sensitive, so take care when typing in
the commands!
– library(affy) works, but Library(affy) does
not.
• Multiple commands can be written on the same
line. Here we first remove missing values from
the variable year, and then calculate it’s
arithmetic average.
– Writing:
• na.omit(year)
• mean(year)
– Would be the same as
• mean(na.omit(year))
Functions or commands in R 3/3
• Command can have many arguments.
These are always giving inside the
brackets.
• Numeric (1, 2, 3…) or logic (T/F) values
and names of existing objects are given
for the arguments without quotes, but
string values, such as file names, are
always put inside quotes. For example:
• mas5(dat3, normalize=T, analysis=”absolute”)
Data structures 1/6
• Vector
– A list of numbers, such as (1,2,3,4,5)
– R: a<-c(1,2,3,4,5)
• Command c creates a vector that is assigned to object a
• Factor
– A list of levels, either numeric or string
– R: b<-as.factor(a)
• Vector a is converted into a factor
Data structures 2/6
• Data frame
– A table where columns can contain numeric
and string values
– R: d<-data.frame(a, b)
• Matrix
– All columns must contain either numeric or string
values, but these can not be combined
– R: e<-as.matrix(d)
• Data frame d is converted into a matrix e
– R: f<-as.data.frame(e)
• Matrix e is converted into a dataframe f
Data structures 3/6
• List
– Contains a list of objects of possibly different
types.
– R: g<-as.list(d)
• Converts a data frame d into a list g
• Class structures
– Many of the Bioconductor functions create a
formal class structure, such as an AffyBatch
object.
– They contain data in slots
– Slots can be accessed using the @-operator:
• dat2@cdfName
Data structures 4/6
• Some command need to get, for example, a
matrix, and do not accept a data frame. Data
frame would give an error message.
• To check the object type:
– R: class(d)
• To check what fields there are in the object:
– R: d
– R: str(d)
• To check the size of the table/matrix:
– R: dim(d)
• To check the length of a factor of vector:
– R: length(a)
Data structures 5/6
• Some data frame related commands:
– R: names(d)
• Reports column names
– R: row.names(d)
• Reports row names
• These can also be used for giving the names for the
data frame. For example:
– R: row.names(d)<-c("a","b","c","d","e")
• Letters from a to e are used as the row names for data frame d
• Note the quotes around the string values!
– R: row.names(d)
Data structures 5/6
• Naming objects:
– Never use command names as object names!
– If your unsure whether something is a command
name, type to the comman line first. If it gives an error
message, you’re safe to use it.
– Object names can’t start with a number
– Never use special characters, such as å, ä, or ö in
object names.
– Underscore (_) is not usable, use dot (.) instead:
• Not acceptable: good_data
• Better way: good.data
– Object names are case sensitive, just like commands
Reading data 1/2
• Command for reading in text files is:
read.table(”suomi.txt”, header=T, sep=”t”)
• This examples has one command with three
arguments: file name (in quotes), header that
tells whether columns have titles, and sep that
tells that the file is tab-delimited.
Reading data 2/2
• It is customary to save the data in an object in
R. This is done with the assignment operator
(<-):
dat<-read.table(”suomi.txt”, header=T, sep=”t”)
• Here, the data read from file suomi.txt is saved
in an object dat in R memory.
• The name of the object is on the left and what
is assigned to the object is on the right.
• Command read.table( ) creates a data
frame.
Using data frames
• Individual columns in the data frame can be
accessed using one of the following ways:
– Use its name:
• dat$year
• dat is the data frame, and year is the header of one of its columns.
Dollar sign ($) is an opertaor that accesses that column.
– Split the data frame into variables, and use the names
directly:
• attach(dat)
• year
– Use subscripts
Subscripts 1/2
• Subscripts are given inside square
brackets after the object’s name:
– dat[,1]
• Gets the first column from the object dat
– dat[,1]
• Gets the first row from the object dat
– dat[1,1]
• Gets the first row and it’s first column from the
object dat
• Note that dat is now an object, not a
command!
Subscripts 2/2
• Subscripts can be used for, e.g., extracting a
subset of the data:
– dat[which(dat$year>1900),]
• Now, this takes a bit of pondering to work out…
• First we have the object dat, and we are accessing a part of it,
because it’s name is followed by the square brackets
• Then we have one command (which) that makes an evaluation
whether the column year in the object dat has a value higher than
1900.
• Last the subscript ends with a comma, that tells us that we are
accessing rows.
• So this command takes all the rows that have a year higher 1900
from the object dat that is a data frame.
Writing tables
• To write a table:
– write.table(dat, ”dat.txt”, sep=”t”)
– Here an object dat is written to a file called dat.txt. This file should be
tab-delimited (argument sep).
• To capture what is written on the screen:
– sink(”output.txt”)
– dat
– sink( )
– Here, output written on the screen should be written to a file output.txt
instead. Contents of the object dat are written to the named file. Last,
the file is closed.
– Note that if you accidentally omit the last command, you’ll not be able
to see any output on the screen, because output is still redirected to a
file!
Quitting R
• Use command q() or menu choise File->Exit.
• R asks whether to save workspace image. If you
do, all the object currently in R memory are
written to a file .Rdata, and all command will be
written a file .Rhistory.
• These can be loaded later, and you can continue
your work from where you left it.
• Loading can be done after starting R using the
manu choises File->Load Workspace and File->
Load History.
In summary 1/2
• Commands can be recognized from the brackets ”( )” that follow
them. If you calculate how many bracket pairs there are, you’ll be
able to identify the number of commands.
– pData(dat)<-pd
• Assignment to an object is denoted by ”<-” or ”->” or ”=”. If you see a
notation ”= =”, you’ll looking at a comparison operator.
– Many other notations can be found from the documentation for the Base
package or R.
• Table-like objects are often followed by square brackets ”[ ]”. Square
never associate with commands, only objects.
– dat[,1]
• Special characters $ and @ are used denoting individual columns in
a data frame or an individual slot in a class type of an object,
respectively.
– dat$year
– dat2@cdfName
In summary 2/2
• If you encounter a new command during the exercises,
and you’d like to know what it does, please consult the
documentation. All R commands are listed nowhere, and
the only way to get to know new commands is to read the
documentation files, so we’d like you to practise this
youself.
• You’ll probably see command and notations that were not
introduced in this talk. This in intentional, because we
thought that these things are best handled on a
situational basis. In such cases, please ask for more
clarifications if needed.
• If you run into problems, please ask for help from the
teachers. That’s why we are here!
Downloading R
Downloading R
Downloading R
Downloading R
Downloading R
Installing R for Windows
• Execute the R-2.3.0-win32.exe with
administrator privileges
• Once the program is installed, run the R
program by clicking on its icon
• R 2.2.1 with Bioconductor 1.7.0 is installed
on corona.csc.fi, also
• R 2.3.1 is in works
Downloading Bioconductor
Installing Bioconductor
Installing Bioconductor
Installing Bioconductor
Installing Bioconductor
Installing Bioconductor (the best way)
• Alternatively, you can install Bioconductor
using a script:
source("http://www.bioconductor.org/biocLite.R")
biocLite()
biocLite(c(” "hgu133a", "hgu133acdf",
"hgu133aprobe", "ygs98", "ygs98cdf",
"ygs98probe")

More Related Content

PDF
محاضرة برنامج التحليل الكمي R program د.هديل القفيدي
PDF
محاضرة برنامج التحليل الكمي R program د.هديل القفيدي
PDF
R Traning-Session-I 21-23 May 2025 Updated Alpha.pdf
PPT
Slides on introduction to R by ArinBasu MD
PPT
Basics of R-Progranmming with instata.ppt
PPT
PPT
17641.ppt
PPT
introduction to R with example, Data science
محاضرة برنامج التحليل الكمي R program د.هديل القفيدي
محاضرة برنامج التحليل الكمي R program د.هديل القفيدي
R Traning-Session-I 21-23 May 2025 Updated Alpha.pdf
Slides on introduction to R by ArinBasu MD
Basics of R-Progranmming with instata.ppt
17641.ppt
introduction to R with example, Data science

Similar to Basics R.ppt (20)

PDF
Data analystics with R module 3 cseds vtu
PPT
Introduction to R for Data Science Technology
PPT
How to obtain and install R.ppt
PPTX
Introduction to R _IMPORTANT FOR DATA ANALYTICS
PPTX
Introduction To Programming In R for data analyst
PPT
Advanced Data Analytics with R Programming.ppt
PPTX
RPreliminariesdsjhfsdsfhjshfjsdhjfhjfhdfjhf
PPTX
RPreliminariesdsjhfsdsfhjshfjsdhjfhjfhdfjhf
PPTX
R Introduction
PPTX
R Get Started I
PPTX
PPTX
Introduction to R programming Language.pptx
PPTX
Aggregate.pptx
PPTX
Introduction to R for Learning Analytics Researchers
PPTX
Introduction to R - Basics of R programming, Data structures.pptx
PDF
Basics of R programming for analytics [Autosaved] (1).pdf
PPTX
R - Get Started I - Sanaitics
PPTX
Programming with R in Big Data Analytics
PPTX
Unit I - 1R introduction to R program.pptx
PPTX
Data Handling in R language basic concepts.pptx
Data analystics with R module 3 cseds vtu
Introduction to R for Data Science Technology
How to obtain and install R.ppt
Introduction to R _IMPORTANT FOR DATA ANALYTICS
Introduction To Programming In R for data analyst
Advanced Data Analytics with R Programming.ppt
RPreliminariesdsjhfsdsfhjshfjsdhjfhjfhdfjhf
RPreliminariesdsjhfsdsfhjshfjsdhjfhjfhdfjhf
R Introduction
R Get Started I
Introduction to R programming Language.pptx
Aggregate.pptx
Introduction to R for Learning Analytics Researchers
Introduction to R - Basics of R programming, Data structures.pptx
Basics of R programming for analytics [Autosaved] (1).pdf
R - Get Started I - Sanaitics
Programming with R in Big Data Analytics
Unit I - 1R introduction to R program.pptx
Data Handling in R language basic concepts.pptx
Ad

Recently uploaded (20)

PDF
Understanding Forklifts - TECH EHS Solution
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
top salesforce developer skills in 2025.pdf
PPTX
Introduction to Artificial Intelligence
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PPTX
ai tools demonstartion for schools and inter college
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
Digital Strategies for Manufacturing Companies
PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PPTX
Odoo POS Development Services by CandidRoot Solutions
Understanding Forklifts - TECH EHS Solution
How to Migrate SBCGlobal Email to Yahoo Easily
PTS Company Brochure 2025 (1).pdf.......
How to Choose the Right IT Partner for Your Business in Malaysia
top salesforce developer skills in 2025.pdf
Introduction to Artificial Intelligence
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
ai tools demonstartion for schools and inter college
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Wondershare Filmora 15 Crack With Activation Key [2025
Navsoft: AI-Powered Business Solutions & Custom Software Development
Which alternative to Crystal Reports is best for small or large businesses.pdf
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
2025 Textile ERP Trends: SAP, Odoo & Oracle
Digital Strategies for Manufacturing Companies
How Creative Agencies Leverage Project Management Software.pdf
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
Odoo POS Development Services by CandidRoot Solutions
Ad

Basics R.ppt

  • 1. Basic of R language Name: Roll No.
  • 2. Learning aims • Basic use of R and R help • How to give R commands • R data structures • Reading and writing data • Some more R commands (exercises)
  • 3. 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
  • 4. Packages • R consists of a core and packages. • Packages contain functions that are not available in the core. • For example, Bioconductor code is distributed as several dozen of packages for R. – Software packages – Metadata (annotation) packages
  • 7. Help - Search engine
  • 9. Anatomy of a help file 1/2 Function {package} General description Command and it’s argument Detailed description of arguments
  • 10. Anatomy of a help file 2/2 Description of how function actually works What function returns Related functions Examples, can be run from R by: example(mas5)
  • 11. Functions or commands in R 1/3 • To use a function in a package, the package needs to be loaded in memory. • Command for this is library( ), for example: library(affy) • There are three parts in a command: – the command – brackets – Arguments inside brackets (these are not always present)
  • 12. Functions or commands in R 2/3 • R is case sensitive, so take care when typing in the commands! – library(affy) works, but Library(affy) does not. • Multiple commands can be written on the same line. Here we first remove missing values from the variable year, and then calculate it’s arithmetic average. – Writing: • na.omit(year) • mean(year) – Would be the same as • mean(na.omit(year))
  • 13. Functions or commands in R 3/3 • Command can have many arguments. These are always giving inside the brackets. • Numeric (1, 2, 3…) or logic (T/F) values and names of existing objects are given for the arguments without quotes, but string values, such as file names, are always put inside quotes. For example: • mas5(dat3, normalize=T, analysis=”absolute”)
  • 14. Data structures 1/6 • Vector – A list of numbers, such as (1,2,3,4,5) – R: a<-c(1,2,3,4,5) • Command c creates a vector that is assigned to object a • Factor – A list of levels, either numeric or string – R: b<-as.factor(a) • Vector a is converted into a factor
  • 15. Data structures 2/6 • Data frame – A table where columns can contain numeric and string values – R: d<-data.frame(a, b) • Matrix – All columns must contain either numeric or string values, but these can not be combined – R: e<-as.matrix(d) • Data frame d is converted into a matrix e – R: f<-as.data.frame(e) • Matrix e is converted into a dataframe f
  • 16. Data structures 3/6 • List – Contains a list of objects of possibly different types. – R: g<-as.list(d) • Converts a data frame d into a list g • Class structures – Many of the Bioconductor functions create a formal class structure, such as an AffyBatch object. – They contain data in slots – Slots can be accessed using the @-operator: • dat2@cdfName
  • 17. Data structures 4/6 • Some command need to get, for example, a matrix, and do not accept a data frame. Data frame would give an error message. • To check the object type: – R: class(d) • To check what fields there are in the object: – R: d – R: str(d) • To check the size of the table/matrix: – R: dim(d) • To check the length of a factor of vector: – R: length(a)
  • 18. Data structures 5/6 • Some data frame related commands: – R: names(d) • Reports column names – R: row.names(d) • Reports row names • These can also be used for giving the names for the data frame. For example: – R: row.names(d)<-c("a","b","c","d","e") • Letters from a to e are used as the row names for data frame d • Note the quotes around the string values! – R: row.names(d)
  • 19. Data structures 5/6 • Naming objects: – Never use command names as object names! – If your unsure whether something is a command name, type to the comman line first. If it gives an error message, you’re safe to use it. – Object names can’t start with a number – Never use special characters, such as å, ä, or ö in object names. – Underscore (_) is not usable, use dot (.) instead: • Not acceptable: good_data • Better way: good.data – Object names are case sensitive, just like commands
  • 20. Reading data 1/2 • Command for reading in text files is: read.table(”suomi.txt”, header=T, sep=”t”) • This examples has one command with three arguments: file name (in quotes), header that tells whether columns have titles, and sep that tells that the file is tab-delimited.
  • 21. Reading data 2/2 • It is customary to save the data in an object in R. This is done with the assignment operator (<-): dat<-read.table(”suomi.txt”, header=T, sep=”t”) • Here, the data read from file suomi.txt is saved in an object dat in R memory. • The name of the object is on the left and what is assigned to the object is on the right. • Command read.table( ) creates a data frame.
  • 22. Using data frames • Individual columns in the data frame can be accessed using one of the following ways: – Use its name: • dat$year • dat is the data frame, and year is the header of one of its columns. Dollar sign ($) is an opertaor that accesses that column. – Split the data frame into variables, and use the names directly: • attach(dat) • year – Use subscripts
  • 23. Subscripts 1/2 • Subscripts are given inside square brackets after the object’s name: – dat[,1] • Gets the first column from the object dat – dat[,1] • Gets the first row from the object dat – dat[1,1] • Gets the first row and it’s first column from the object dat • Note that dat is now an object, not a command!
  • 24. Subscripts 2/2 • Subscripts can be used for, e.g., extracting a subset of the data: – dat[which(dat$year>1900),] • Now, this takes a bit of pondering to work out… • First we have the object dat, and we are accessing a part of it, because it’s name is followed by the square brackets • Then we have one command (which) that makes an evaluation whether the column year in the object dat has a value higher than 1900. • Last the subscript ends with a comma, that tells us that we are accessing rows. • So this command takes all the rows that have a year higher 1900 from the object dat that is a data frame.
  • 25. Writing tables • To write a table: – write.table(dat, ”dat.txt”, sep=”t”) – Here an object dat is written to a file called dat.txt. This file should be tab-delimited (argument sep). • To capture what is written on the screen: – sink(”output.txt”) – dat – sink( ) – Here, output written on the screen should be written to a file output.txt instead. Contents of the object dat are written to the named file. Last, the file is closed. – Note that if you accidentally omit the last command, you’ll not be able to see any output on the screen, because output is still redirected to a file!
  • 26. Quitting R • Use command q() or menu choise File->Exit. • R asks whether to save workspace image. If you do, all the object currently in R memory are written to a file .Rdata, and all command will be written a file .Rhistory. • These can be loaded later, and you can continue your work from where you left it. • Loading can be done after starting R using the manu choises File->Load Workspace and File-> Load History.
  • 27. In summary 1/2 • Commands can be recognized from the brackets ”( )” that follow them. If you calculate how many bracket pairs there are, you’ll be able to identify the number of commands. – pData(dat)<-pd • Assignment to an object is denoted by ”<-” or ”->” or ”=”. If you see a notation ”= =”, you’ll looking at a comparison operator. – Many other notations can be found from the documentation for the Base package or R. • Table-like objects are often followed by square brackets ”[ ]”. Square never associate with commands, only objects. – dat[,1] • Special characters $ and @ are used denoting individual columns in a data frame or an individual slot in a class type of an object, respectively. – dat$year – dat2@cdfName
  • 28. In summary 2/2 • If you encounter a new command during the exercises, and you’d like to know what it does, please consult the documentation. All R commands are listed nowhere, and the only way to get to know new commands is to read the documentation files, so we’d like you to practise this youself. • You’ll probably see command and notations that were not introduced in this talk. This in intentional, because we thought that these things are best handled on a situational basis. In such cases, please ask for more clarifications if needed. • If you run into problems, please ask for help from the teachers. That’s why we are here!
  • 34. Installing R for Windows • Execute the R-2.3.0-win32.exe with administrator privileges • Once the program is installed, run the R program by clicking on its icon • R 2.2.1 with Bioconductor 1.7.0 is installed on corona.csc.fi, also • R 2.3.1 is in works
  • 40. Installing Bioconductor (the best way) • Alternatively, you can install Bioconductor using a script: source("http://www.bioconductor.org/biocLite.R") biocLite() biocLite(c(” "hgu133a", "hgu133acdf", "hgu133aprobe", "ygs98", "ygs98cdf", "ygs98probe")