an introduction to r
DESCRIPTION
An Introduction to R. 96325125 鐘英愷 93316150 劉郁彧 93316105 梁詩屏 93316113 陳泓君. Outline. The R environment R and Statistics Data Import/export Basic Operator Programming in R Graphics. The R environment. - PowerPoint PPT PresentationTRANSCRIPT
An Introduction to R
96325125 鐘英愷93316150 劉郁彧93316105 梁詩屏93316113 陳泓君
Outline
The R environment
R and Statistics
Data Importexport
Basic Operator
Programming in R
Graphics
The R environment
R is an integrated suite of software facilities for data manipulation calculation and graphical display Among other things it has
an effective data handling and storage facility a suite of operators for calculations on arrays in particular matrices a large coherent integrated collection of intermediate tools for data analysis graphical facilities for data analysis and display either directly at the computer or on hard-copy and a well developed simple and effective programming language (called `S) which includes conditionals loops user defined recursive functions and input and output facilities (Indeed most of the system supplied functions are themselves written in the S language)
The term ldquoenvironment is intended to characterize it as a fully planned and coherent system rather than an incremental accretion of very specific and inflexible tools as is frequently the case with other data analysis software
R is a newly developing methods of interactive data analysis It has developed rapidly and has been extended by a large collection of packages
R with Statistics
R is a system for statistical analyses and graphics created by Ross Ihaka and Robert Gentleman
R is both a software and a language considered as a dialect of the language S created by the ATampT Bell Laboratories S is available as the software S-PLUS commercialized by Insightful
How it makeamp where we get it
R is available in several forms the sources written mainly in C (and some routines in Fortran) essentially for Unix and Linux machines or some pre-compiled binaries for Windows Linux (Debian Mandrake RedHat SuSe) Macintosh and Alpha UnixThe files needed to install R either from the sources or from the pre-compiled binaries are distributed from the internet site of the Comprehensive R Archive Network (CRAN) where the instructions for the installation are also available httpCRANR-projectorg
R has many functions for statistical analyses and graphics the latter are visualized immediately in their own window and can be saved in various formats (jpg png bmp ps pdf emf pictex xfig) The results from a statistical analysis are displayed on the screen some intermediate results (P-values regression coefficients residuals ) can be saved written in a file or used in subsequent analyses
There is an important difference between S (and hence R) and the other main statistical systems In S a statistical analysis is normally done as a series of steps with intermediate results being stored in objectsSAS and SPSS will give copious output from a regression or discriminant analysis R will give minimal output and store the results in a t object for subsequent interrogation by further R functions
Disadvantages of R
R is not efficient in handling large data sets
Slow computation for a large number of do-loops compared to CC++ Fortran etc
Self-Learning is not so convenient compared to ldquopoint-and-clickrdquo statistics software
No warrantee and informal support
Needed to upgrade R version to install some newly developed packages
Getting Help in R
library()lists all available libraries on system
help(command)getting help for one command eg help(heatmap)
helpsearch(ldquotopicrdquo)searches help system for packages associated with the topic eg helpsearch(ldquodistributionrdquo)
helpstart()starts local HTML interface
q()quits R console
Basic Usage of R
The general R command syntax uses the assignment operator ldquolt-rdquo(or ldquo=ldquo) to assign data to objectobject lt- function (arguments)
source(ldquomyscriptRrdquo) command to execute an R script named as myscriptRobjects()or ls() list the names of all objects rm(data1) Remove the object named data1 from the current environment data1 lt-edit(dataframe()) Starts empty GUI spreadsheet editor for manual data entry
Basic Usage of R
class(object)displays the object type
str(object)displays the internal type and structure of an R objectgtstr(m)num [14 13] 0248 0589 -0589 0504 1524
attributes(object)Returns an objects attribute listgt attributes(m)$dim[1] 4 3
dir() Reads content of current working directory
getwd() Returns current working directory
setwd(homeuser) Changes current working directory to user specified directory
Data Import
readdelim(clipboard header=T) Command to copyamppastetables from Excel or other programs into R If the header argument isset to FALSE then the first line of the data set will not be used as column titlesscan(my_file) reads vectorarray into vector from file or keyboardmy_framelt-readdelim(ldquocAffymetrixaffy1txt nastrings= fill=TRUE header=T sep=t) The function readdelim() is often more flexible for importing tables with empty fields and long character strings (eg gene descriptions)
It supports data import on the webDifferent coding of missing values (nastrings=ldquoNArdquoor ldquordquo)
Data columns can be separated by TAB comma or semicolon (sep=ldquordquo)
Data Import
There are some alternatives for reading data as followingsmy_framelt-readtable(file=ldquopath header=TRUE sep=t)Reads in table with info on column headers and field separators datalt-readtable(httpwwwcmuedutwexampletxt header=TRUE)
my_framelt-readcsv(file=ldquopathldquo header=TRUE) reads csvfile with comma separated value You can skip lines read a limited number of lines different decimal separator and more importing options The foreign package can read files from Stata SAS and SPSS
Data Export
writetable(iris clipboard sep=t colnames=NA quote=F) Command to copyamppastefrom R into Excel or other programs It writes the data of an R data frame object into the clipbroardfrom where it can be pasted into other applicationswritetable(dataframe file=ldquofile path sep=t colnames= NA) Writes data frame to a tab-delimited text file The argument colnames= NA makes sure that the titles align with columns when rowindex names are exported (default)write(x file=file path) Writes matrix data to a file
Basic Operators
Comparison operators equal == not equal = greaterless than gt lt greaterless than or equal gt= lt= Example 1 == 1 Returns TRUE
1048710Logical operators ANDamp
x lt- 110 y lt- 101 Creates the sample vectors x and y x gt y amp x gt 5 Returns TRUE where both comparisons return TRUE OR |
x == y | x = y Returns TRUE where at least one comparison returns TRUE NOT
x gt y The sign returns the negation (opposite) of a logical vector
Basic Operators
Calculations Four basic arithmetic functions addition subtraction multiplication and division 1 + 1 1 - 1 1 1 1 1 Returns the results of these calculations
Calculations on vectors x lt- 120 sum(x) mean(x) sd(x) sqrt(x) rank(x)sort(x) Calculates for the vector x its sum mean standard deviation and square root etc x lt- 120 y lt- 120 x + y Calculates the sum for each element in the vectors x and y
Data Types
Numeric data 1 2 3 x lt- c(1 2 3) x isnumeric(x) ascharacter(x) Creates a numeric vector checks for the data type and converts it into a character vectorCharacter data a b c x lt- c(1 2 3) x ischaracter(x) asnumeric(x)
Creates a character vector checks for the data type and converts it into a numeric vectorLogical data TRUE FALSE TRUE 110 lt 5 Returns TRUE where x is lt 5
Object Types
vectors ordered collection of numeric character complex and logical values factors special type vectors with grouping information of its components data frames two dimensional structures with different data types matrices two dimensional structures with data of same type arrays multidimensional arrays of vectors lists general form of vectors with different types of elements functions piece of code
Subsetting Syntax
my_object[index] Subsettingof one dimensional objects like vectors and factors Returns elements with positions in indexmy_object[rowindex colindex] Subsettingof two dimensional objects like matrices and data framesmy_object[rowindex colindex dim] Subsettingof three dimensional objects like arraysdim(my_object) Returns the numbers of row and columnmy_logicallt-(my_objectgt 10) Generates a logical vector as examplemy_object[my_logical] Returns the elements where my_logical contains TRUE valuesmy_object$Name1 Returns the lsquoName1 column in the my_objectdata frame
Vector amp List
bull vector an ordered collection of data of the same type gt a = c(751)gt a[2][1] 5
bull list an ordered collection of data of arbitrary types gt doe = list(name=johnage=28married=F)gt doe$name[1] johnldquogt doe$age[1] 28bull Typically vector elements are accessed by their index (an integer)
list elements by their name (a character string) But both types support both access methods
Programming in R
Ifelse StatementExample x lt- 110 ifelse(xlt5 | xgt8 x 0)
For Loop Example mean mydf lt- irismyve lt- NULLfor(i in 1length(mydf[1])) myve lt- c(myve mean(asvector(asmatrix(mydf[i1
3]))))
While Loop Example z lt- 0while(zlt5) z lt- z+2print(z)
Writing your own functionsgt f=function(x)x^2+2xgtf(3)[1] 15
Graphics
library(UsingR)scatterwithhist(faithful$eruptionsfaithf ul$waiting)
Reference
2007 年 R 統計軟體研習會 ------- 蔡政安 教授httpwwwstateduntuedutw2007conferenceindexhtm
An Introduction to R
httpcranr-projectorgdocmanualsR-intropdf
~The End~
Thanks for your listening
Outline
The R environment
R and Statistics
Data Importexport
Basic Operator
Programming in R
Graphics
The R environment
R is an integrated suite of software facilities for data manipulation calculation and graphical display Among other things it has
an effective data handling and storage facility a suite of operators for calculations on arrays in particular matrices a large coherent integrated collection of intermediate tools for data analysis graphical facilities for data analysis and display either directly at the computer or on hard-copy and a well developed simple and effective programming language (called `S) which includes conditionals loops user defined recursive functions and input and output facilities (Indeed most of the system supplied functions are themselves written in the S language)
The term ldquoenvironment is intended to characterize it as a fully planned and coherent system rather than an incremental accretion of very specific and inflexible tools as is frequently the case with other data analysis software
R is a newly developing methods of interactive data analysis It has developed rapidly and has been extended by a large collection of packages
R with Statistics
R is a system for statistical analyses and graphics created by Ross Ihaka and Robert Gentleman
R is both a software and a language considered as a dialect of the language S created by the ATampT Bell Laboratories S is available as the software S-PLUS commercialized by Insightful
How it makeamp where we get it
R is available in several forms the sources written mainly in C (and some routines in Fortran) essentially for Unix and Linux machines or some pre-compiled binaries for Windows Linux (Debian Mandrake RedHat SuSe) Macintosh and Alpha UnixThe files needed to install R either from the sources or from the pre-compiled binaries are distributed from the internet site of the Comprehensive R Archive Network (CRAN) where the instructions for the installation are also available httpCRANR-projectorg
R has many functions for statistical analyses and graphics the latter are visualized immediately in their own window and can be saved in various formats (jpg png bmp ps pdf emf pictex xfig) The results from a statistical analysis are displayed on the screen some intermediate results (P-values regression coefficients residuals ) can be saved written in a file or used in subsequent analyses
There is an important difference between S (and hence R) and the other main statistical systems In S a statistical analysis is normally done as a series of steps with intermediate results being stored in objectsSAS and SPSS will give copious output from a regression or discriminant analysis R will give minimal output and store the results in a t object for subsequent interrogation by further R functions
Disadvantages of R
R is not efficient in handling large data sets
Slow computation for a large number of do-loops compared to CC++ Fortran etc
Self-Learning is not so convenient compared to ldquopoint-and-clickrdquo statistics software
No warrantee and informal support
Needed to upgrade R version to install some newly developed packages
Getting Help in R
library()lists all available libraries on system
help(command)getting help for one command eg help(heatmap)
helpsearch(ldquotopicrdquo)searches help system for packages associated with the topic eg helpsearch(ldquodistributionrdquo)
helpstart()starts local HTML interface
q()quits R console
Basic Usage of R
The general R command syntax uses the assignment operator ldquolt-rdquo(or ldquo=ldquo) to assign data to objectobject lt- function (arguments)
source(ldquomyscriptRrdquo) command to execute an R script named as myscriptRobjects()or ls() list the names of all objects rm(data1) Remove the object named data1 from the current environment data1 lt-edit(dataframe()) Starts empty GUI spreadsheet editor for manual data entry
Basic Usage of R
class(object)displays the object type
str(object)displays the internal type and structure of an R objectgtstr(m)num [14 13] 0248 0589 -0589 0504 1524
attributes(object)Returns an objects attribute listgt attributes(m)$dim[1] 4 3
dir() Reads content of current working directory
getwd() Returns current working directory
setwd(homeuser) Changes current working directory to user specified directory
Data Import
readdelim(clipboard header=T) Command to copyamppastetables from Excel or other programs into R If the header argument isset to FALSE then the first line of the data set will not be used as column titlesscan(my_file) reads vectorarray into vector from file or keyboardmy_framelt-readdelim(ldquocAffymetrixaffy1txt nastrings= fill=TRUE header=T sep=t) The function readdelim() is often more flexible for importing tables with empty fields and long character strings (eg gene descriptions)
It supports data import on the webDifferent coding of missing values (nastrings=ldquoNArdquoor ldquordquo)
Data columns can be separated by TAB comma or semicolon (sep=ldquordquo)
Data Import
There are some alternatives for reading data as followingsmy_framelt-readtable(file=ldquopath header=TRUE sep=t)Reads in table with info on column headers and field separators datalt-readtable(httpwwwcmuedutwexampletxt header=TRUE)
my_framelt-readcsv(file=ldquopathldquo header=TRUE) reads csvfile with comma separated value You can skip lines read a limited number of lines different decimal separator and more importing options The foreign package can read files from Stata SAS and SPSS
Data Export
writetable(iris clipboard sep=t colnames=NA quote=F) Command to copyamppastefrom R into Excel or other programs It writes the data of an R data frame object into the clipbroardfrom where it can be pasted into other applicationswritetable(dataframe file=ldquofile path sep=t colnames= NA) Writes data frame to a tab-delimited text file The argument colnames= NA makes sure that the titles align with columns when rowindex names are exported (default)write(x file=file path) Writes matrix data to a file
Basic Operators
Comparison operators equal == not equal = greaterless than gt lt greaterless than or equal gt= lt= Example 1 == 1 Returns TRUE
1048710Logical operators ANDamp
x lt- 110 y lt- 101 Creates the sample vectors x and y x gt y amp x gt 5 Returns TRUE where both comparisons return TRUE OR |
x == y | x = y Returns TRUE where at least one comparison returns TRUE NOT
x gt y The sign returns the negation (opposite) of a logical vector
Basic Operators
Calculations Four basic arithmetic functions addition subtraction multiplication and division 1 + 1 1 - 1 1 1 1 1 Returns the results of these calculations
Calculations on vectors x lt- 120 sum(x) mean(x) sd(x) sqrt(x) rank(x)sort(x) Calculates for the vector x its sum mean standard deviation and square root etc x lt- 120 y lt- 120 x + y Calculates the sum for each element in the vectors x and y
Data Types
Numeric data 1 2 3 x lt- c(1 2 3) x isnumeric(x) ascharacter(x) Creates a numeric vector checks for the data type and converts it into a character vectorCharacter data a b c x lt- c(1 2 3) x ischaracter(x) asnumeric(x)
Creates a character vector checks for the data type and converts it into a numeric vectorLogical data TRUE FALSE TRUE 110 lt 5 Returns TRUE where x is lt 5
Object Types
vectors ordered collection of numeric character complex and logical values factors special type vectors with grouping information of its components data frames two dimensional structures with different data types matrices two dimensional structures with data of same type arrays multidimensional arrays of vectors lists general form of vectors with different types of elements functions piece of code
Subsetting Syntax
my_object[index] Subsettingof one dimensional objects like vectors and factors Returns elements with positions in indexmy_object[rowindex colindex] Subsettingof two dimensional objects like matrices and data framesmy_object[rowindex colindex dim] Subsettingof three dimensional objects like arraysdim(my_object) Returns the numbers of row and columnmy_logicallt-(my_objectgt 10) Generates a logical vector as examplemy_object[my_logical] Returns the elements where my_logical contains TRUE valuesmy_object$Name1 Returns the lsquoName1 column in the my_objectdata frame
Vector amp List
bull vector an ordered collection of data of the same type gt a = c(751)gt a[2][1] 5
bull list an ordered collection of data of arbitrary types gt doe = list(name=johnage=28married=F)gt doe$name[1] johnldquogt doe$age[1] 28bull Typically vector elements are accessed by their index (an integer)
list elements by their name (a character string) But both types support both access methods
Programming in R
Ifelse StatementExample x lt- 110 ifelse(xlt5 | xgt8 x 0)
For Loop Example mean mydf lt- irismyve lt- NULLfor(i in 1length(mydf[1])) myve lt- c(myve mean(asvector(asmatrix(mydf[i1
3]))))
While Loop Example z lt- 0while(zlt5) z lt- z+2print(z)
Writing your own functionsgt f=function(x)x^2+2xgtf(3)[1] 15
Graphics
library(UsingR)scatterwithhist(faithful$eruptionsfaithf ul$waiting)
Reference
2007 年 R 統計軟體研習會 ------- 蔡政安 教授httpwwwstateduntuedutw2007conferenceindexhtm
An Introduction to R
httpcranr-projectorgdocmanualsR-intropdf
~The End~
Thanks for your listening
The R environment
R is an integrated suite of software facilities for data manipulation calculation and graphical display Among other things it has
an effective data handling and storage facility a suite of operators for calculations on arrays in particular matrices a large coherent integrated collection of intermediate tools for data analysis graphical facilities for data analysis and display either directly at the computer or on hard-copy and a well developed simple and effective programming language (called `S) which includes conditionals loops user defined recursive functions and input and output facilities (Indeed most of the system supplied functions are themselves written in the S language)
The term ldquoenvironment is intended to characterize it as a fully planned and coherent system rather than an incremental accretion of very specific and inflexible tools as is frequently the case with other data analysis software
R is a newly developing methods of interactive data analysis It has developed rapidly and has been extended by a large collection of packages
R with Statistics
R is a system for statistical analyses and graphics created by Ross Ihaka and Robert Gentleman
R is both a software and a language considered as a dialect of the language S created by the ATampT Bell Laboratories S is available as the software S-PLUS commercialized by Insightful
How it makeamp where we get it
R is available in several forms the sources written mainly in C (and some routines in Fortran) essentially for Unix and Linux machines or some pre-compiled binaries for Windows Linux (Debian Mandrake RedHat SuSe) Macintosh and Alpha UnixThe files needed to install R either from the sources or from the pre-compiled binaries are distributed from the internet site of the Comprehensive R Archive Network (CRAN) where the instructions for the installation are also available httpCRANR-projectorg
R has many functions for statistical analyses and graphics the latter are visualized immediately in their own window and can be saved in various formats (jpg png bmp ps pdf emf pictex xfig) The results from a statistical analysis are displayed on the screen some intermediate results (P-values regression coefficients residuals ) can be saved written in a file or used in subsequent analyses
There is an important difference between S (and hence R) and the other main statistical systems In S a statistical analysis is normally done as a series of steps with intermediate results being stored in objectsSAS and SPSS will give copious output from a regression or discriminant analysis R will give minimal output and store the results in a t object for subsequent interrogation by further R functions
Disadvantages of R
R is not efficient in handling large data sets
Slow computation for a large number of do-loops compared to CC++ Fortran etc
Self-Learning is not so convenient compared to ldquopoint-and-clickrdquo statistics software
No warrantee and informal support
Needed to upgrade R version to install some newly developed packages
Getting Help in R
library()lists all available libraries on system
help(command)getting help for one command eg help(heatmap)
helpsearch(ldquotopicrdquo)searches help system for packages associated with the topic eg helpsearch(ldquodistributionrdquo)
helpstart()starts local HTML interface
q()quits R console
Basic Usage of R
The general R command syntax uses the assignment operator ldquolt-rdquo(or ldquo=ldquo) to assign data to objectobject lt- function (arguments)
source(ldquomyscriptRrdquo) command to execute an R script named as myscriptRobjects()or ls() list the names of all objects rm(data1) Remove the object named data1 from the current environment data1 lt-edit(dataframe()) Starts empty GUI spreadsheet editor for manual data entry
Basic Usage of R
class(object)displays the object type
str(object)displays the internal type and structure of an R objectgtstr(m)num [14 13] 0248 0589 -0589 0504 1524
attributes(object)Returns an objects attribute listgt attributes(m)$dim[1] 4 3
dir() Reads content of current working directory
getwd() Returns current working directory
setwd(homeuser) Changes current working directory to user specified directory
Data Import
readdelim(clipboard header=T) Command to copyamppastetables from Excel or other programs into R If the header argument isset to FALSE then the first line of the data set will not be used as column titlesscan(my_file) reads vectorarray into vector from file or keyboardmy_framelt-readdelim(ldquocAffymetrixaffy1txt nastrings= fill=TRUE header=T sep=t) The function readdelim() is often more flexible for importing tables with empty fields and long character strings (eg gene descriptions)
It supports data import on the webDifferent coding of missing values (nastrings=ldquoNArdquoor ldquordquo)
Data columns can be separated by TAB comma or semicolon (sep=ldquordquo)
Data Import
There are some alternatives for reading data as followingsmy_framelt-readtable(file=ldquopath header=TRUE sep=t)Reads in table with info on column headers and field separators datalt-readtable(httpwwwcmuedutwexampletxt header=TRUE)
my_framelt-readcsv(file=ldquopathldquo header=TRUE) reads csvfile with comma separated value You can skip lines read a limited number of lines different decimal separator and more importing options The foreign package can read files from Stata SAS and SPSS
Data Export
writetable(iris clipboard sep=t colnames=NA quote=F) Command to copyamppastefrom R into Excel or other programs It writes the data of an R data frame object into the clipbroardfrom where it can be pasted into other applicationswritetable(dataframe file=ldquofile path sep=t colnames= NA) Writes data frame to a tab-delimited text file The argument colnames= NA makes sure that the titles align with columns when rowindex names are exported (default)write(x file=file path) Writes matrix data to a file
Basic Operators
Comparison operators equal == not equal = greaterless than gt lt greaterless than or equal gt= lt= Example 1 == 1 Returns TRUE
1048710Logical operators ANDamp
x lt- 110 y lt- 101 Creates the sample vectors x and y x gt y amp x gt 5 Returns TRUE where both comparisons return TRUE OR |
x == y | x = y Returns TRUE where at least one comparison returns TRUE NOT
x gt y The sign returns the negation (opposite) of a logical vector
Basic Operators
Calculations Four basic arithmetic functions addition subtraction multiplication and division 1 + 1 1 - 1 1 1 1 1 Returns the results of these calculations
Calculations on vectors x lt- 120 sum(x) mean(x) sd(x) sqrt(x) rank(x)sort(x) Calculates for the vector x its sum mean standard deviation and square root etc x lt- 120 y lt- 120 x + y Calculates the sum for each element in the vectors x and y
Data Types
Numeric data 1 2 3 x lt- c(1 2 3) x isnumeric(x) ascharacter(x) Creates a numeric vector checks for the data type and converts it into a character vectorCharacter data a b c x lt- c(1 2 3) x ischaracter(x) asnumeric(x)
Creates a character vector checks for the data type and converts it into a numeric vectorLogical data TRUE FALSE TRUE 110 lt 5 Returns TRUE where x is lt 5
Object Types
vectors ordered collection of numeric character complex and logical values factors special type vectors with grouping information of its components data frames two dimensional structures with different data types matrices two dimensional structures with data of same type arrays multidimensional arrays of vectors lists general form of vectors with different types of elements functions piece of code
Subsetting Syntax
my_object[index] Subsettingof one dimensional objects like vectors and factors Returns elements with positions in indexmy_object[rowindex colindex] Subsettingof two dimensional objects like matrices and data framesmy_object[rowindex colindex dim] Subsettingof three dimensional objects like arraysdim(my_object) Returns the numbers of row and columnmy_logicallt-(my_objectgt 10) Generates a logical vector as examplemy_object[my_logical] Returns the elements where my_logical contains TRUE valuesmy_object$Name1 Returns the lsquoName1 column in the my_objectdata frame
Vector amp List
bull vector an ordered collection of data of the same type gt a = c(751)gt a[2][1] 5
bull list an ordered collection of data of arbitrary types gt doe = list(name=johnage=28married=F)gt doe$name[1] johnldquogt doe$age[1] 28bull Typically vector elements are accessed by their index (an integer)
list elements by their name (a character string) But both types support both access methods
Programming in R
Ifelse StatementExample x lt- 110 ifelse(xlt5 | xgt8 x 0)
For Loop Example mean mydf lt- irismyve lt- NULLfor(i in 1length(mydf[1])) myve lt- c(myve mean(asvector(asmatrix(mydf[i1
3]))))
While Loop Example z lt- 0while(zlt5) z lt- z+2print(z)
Writing your own functionsgt f=function(x)x^2+2xgtf(3)[1] 15
Graphics
library(UsingR)scatterwithhist(faithful$eruptionsfaithf ul$waiting)
Reference
2007 年 R 統計軟體研習會 ------- 蔡政安 教授httpwwwstateduntuedutw2007conferenceindexhtm
An Introduction to R
httpcranr-projectorgdocmanualsR-intropdf
~The End~
Thanks for your listening
The term ldquoenvironment is intended to characterize it as a fully planned and coherent system rather than an incremental accretion of very specific and inflexible tools as is frequently the case with other data analysis software
R is a newly developing methods of interactive data analysis It has developed rapidly and has been extended by a large collection of packages
R with Statistics
R is a system for statistical analyses and graphics created by Ross Ihaka and Robert Gentleman
R is both a software and a language considered as a dialect of the language S created by the ATampT Bell Laboratories S is available as the software S-PLUS commercialized by Insightful
How it makeamp where we get it
R is available in several forms the sources written mainly in C (and some routines in Fortran) essentially for Unix and Linux machines or some pre-compiled binaries for Windows Linux (Debian Mandrake RedHat SuSe) Macintosh and Alpha UnixThe files needed to install R either from the sources or from the pre-compiled binaries are distributed from the internet site of the Comprehensive R Archive Network (CRAN) where the instructions for the installation are also available httpCRANR-projectorg
R has many functions for statistical analyses and graphics the latter are visualized immediately in their own window and can be saved in various formats (jpg png bmp ps pdf emf pictex xfig) The results from a statistical analysis are displayed on the screen some intermediate results (P-values regression coefficients residuals ) can be saved written in a file or used in subsequent analyses
There is an important difference between S (and hence R) and the other main statistical systems In S a statistical analysis is normally done as a series of steps with intermediate results being stored in objectsSAS and SPSS will give copious output from a regression or discriminant analysis R will give minimal output and store the results in a t object for subsequent interrogation by further R functions
Disadvantages of R
R is not efficient in handling large data sets
Slow computation for a large number of do-loops compared to CC++ Fortran etc
Self-Learning is not so convenient compared to ldquopoint-and-clickrdquo statistics software
No warrantee and informal support
Needed to upgrade R version to install some newly developed packages
Getting Help in R
library()lists all available libraries on system
help(command)getting help for one command eg help(heatmap)
helpsearch(ldquotopicrdquo)searches help system for packages associated with the topic eg helpsearch(ldquodistributionrdquo)
helpstart()starts local HTML interface
q()quits R console
Basic Usage of R
The general R command syntax uses the assignment operator ldquolt-rdquo(or ldquo=ldquo) to assign data to objectobject lt- function (arguments)
source(ldquomyscriptRrdquo) command to execute an R script named as myscriptRobjects()or ls() list the names of all objects rm(data1) Remove the object named data1 from the current environment data1 lt-edit(dataframe()) Starts empty GUI spreadsheet editor for manual data entry
Basic Usage of R
class(object)displays the object type
str(object)displays the internal type and structure of an R objectgtstr(m)num [14 13] 0248 0589 -0589 0504 1524
attributes(object)Returns an objects attribute listgt attributes(m)$dim[1] 4 3
dir() Reads content of current working directory
getwd() Returns current working directory
setwd(homeuser) Changes current working directory to user specified directory
Data Import
readdelim(clipboard header=T) Command to copyamppastetables from Excel or other programs into R If the header argument isset to FALSE then the first line of the data set will not be used as column titlesscan(my_file) reads vectorarray into vector from file or keyboardmy_framelt-readdelim(ldquocAffymetrixaffy1txt nastrings= fill=TRUE header=T sep=t) The function readdelim() is often more flexible for importing tables with empty fields and long character strings (eg gene descriptions)
It supports data import on the webDifferent coding of missing values (nastrings=ldquoNArdquoor ldquordquo)
Data columns can be separated by TAB comma or semicolon (sep=ldquordquo)
Data Import
There are some alternatives for reading data as followingsmy_framelt-readtable(file=ldquopath header=TRUE sep=t)Reads in table with info on column headers and field separators datalt-readtable(httpwwwcmuedutwexampletxt header=TRUE)
my_framelt-readcsv(file=ldquopathldquo header=TRUE) reads csvfile with comma separated value You can skip lines read a limited number of lines different decimal separator and more importing options The foreign package can read files from Stata SAS and SPSS
Data Export
writetable(iris clipboard sep=t colnames=NA quote=F) Command to copyamppastefrom R into Excel or other programs It writes the data of an R data frame object into the clipbroardfrom where it can be pasted into other applicationswritetable(dataframe file=ldquofile path sep=t colnames= NA) Writes data frame to a tab-delimited text file The argument colnames= NA makes sure that the titles align with columns when rowindex names are exported (default)write(x file=file path) Writes matrix data to a file
Basic Operators
Comparison operators equal == not equal = greaterless than gt lt greaterless than or equal gt= lt= Example 1 == 1 Returns TRUE
1048710Logical operators ANDamp
x lt- 110 y lt- 101 Creates the sample vectors x and y x gt y amp x gt 5 Returns TRUE where both comparisons return TRUE OR |
x == y | x = y Returns TRUE where at least one comparison returns TRUE NOT
x gt y The sign returns the negation (opposite) of a logical vector
Basic Operators
Calculations Four basic arithmetic functions addition subtraction multiplication and division 1 + 1 1 - 1 1 1 1 1 Returns the results of these calculations
Calculations on vectors x lt- 120 sum(x) mean(x) sd(x) sqrt(x) rank(x)sort(x) Calculates for the vector x its sum mean standard deviation and square root etc x lt- 120 y lt- 120 x + y Calculates the sum for each element in the vectors x and y
Data Types
Numeric data 1 2 3 x lt- c(1 2 3) x isnumeric(x) ascharacter(x) Creates a numeric vector checks for the data type and converts it into a character vectorCharacter data a b c x lt- c(1 2 3) x ischaracter(x) asnumeric(x)
Creates a character vector checks for the data type and converts it into a numeric vectorLogical data TRUE FALSE TRUE 110 lt 5 Returns TRUE where x is lt 5
Object Types
vectors ordered collection of numeric character complex and logical values factors special type vectors with grouping information of its components data frames two dimensional structures with different data types matrices two dimensional structures with data of same type arrays multidimensional arrays of vectors lists general form of vectors with different types of elements functions piece of code
Subsetting Syntax
my_object[index] Subsettingof one dimensional objects like vectors and factors Returns elements with positions in indexmy_object[rowindex colindex] Subsettingof two dimensional objects like matrices and data framesmy_object[rowindex colindex dim] Subsettingof three dimensional objects like arraysdim(my_object) Returns the numbers of row and columnmy_logicallt-(my_objectgt 10) Generates a logical vector as examplemy_object[my_logical] Returns the elements where my_logical contains TRUE valuesmy_object$Name1 Returns the lsquoName1 column in the my_objectdata frame
Vector amp List
bull vector an ordered collection of data of the same type gt a = c(751)gt a[2][1] 5
bull list an ordered collection of data of arbitrary types gt doe = list(name=johnage=28married=F)gt doe$name[1] johnldquogt doe$age[1] 28bull Typically vector elements are accessed by their index (an integer)
list elements by their name (a character string) But both types support both access methods
Programming in R
Ifelse StatementExample x lt- 110 ifelse(xlt5 | xgt8 x 0)
For Loop Example mean mydf lt- irismyve lt- NULLfor(i in 1length(mydf[1])) myve lt- c(myve mean(asvector(asmatrix(mydf[i1
3]))))
While Loop Example z lt- 0while(zlt5) z lt- z+2print(z)
Writing your own functionsgt f=function(x)x^2+2xgtf(3)[1] 15
Graphics
library(UsingR)scatterwithhist(faithful$eruptionsfaithf ul$waiting)
Reference
2007 年 R 統計軟體研習會 ------- 蔡政安 教授httpwwwstateduntuedutw2007conferenceindexhtm
An Introduction to R
httpcranr-projectorgdocmanualsR-intropdf
~The End~
Thanks for your listening
R with Statistics
R is a system for statistical analyses and graphics created by Ross Ihaka and Robert Gentleman
R is both a software and a language considered as a dialect of the language S created by the ATampT Bell Laboratories S is available as the software S-PLUS commercialized by Insightful
How it makeamp where we get it
R is available in several forms the sources written mainly in C (and some routines in Fortran) essentially for Unix and Linux machines or some pre-compiled binaries for Windows Linux (Debian Mandrake RedHat SuSe) Macintosh and Alpha UnixThe files needed to install R either from the sources or from the pre-compiled binaries are distributed from the internet site of the Comprehensive R Archive Network (CRAN) where the instructions for the installation are also available httpCRANR-projectorg
R has many functions for statistical analyses and graphics the latter are visualized immediately in their own window and can be saved in various formats (jpg png bmp ps pdf emf pictex xfig) The results from a statistical analysis are displayed on the screen some intermediate results (P-values regression coefficients residuals ) can be saved written in a file or used in subsequent analyses
There is an important difference between S (and hence R) and the other main statistical systems In S a statistical analysis is normally done as a series of steps with intermediate results being stored in objectsSAS and SPSS will give copious output from a regression or discriminant analysis R will give minimal output and store the results in a t object for subsequent interrogation by further R functions
Disadvantages of R
R is not efficient in handling large data sets
Slow computation for a large number of do-loops compared to CC++ Fortran etc
Self-Learning is not so convenient compared to ldquopoint-and-clickrdquo statistics software
No warrantee and informal support
Needed to upgrade R version to install some newly developed packages
Getting Help in R
library()lists all available libraries on system
help(command)getting help for one command eg help(heatmap)
helpsearch(ldquotopicrdquo)searches help system for packages associated with the topic eg helpsearch(ldquodistributionrdquo)
helpstart()starts local HTML interface
q()quits R console
Basic Usage of R
The general R command syntax uses the assignment operator ldquolt-rdquo(or ldquo=ldquo) to assign data to objectobject lt- function (arguments)
source(ldquomyscriptRrdquo) command to execute an R script named as myscriptRobjects()or ls() list the names of all objects rm(data1) Remove the object named data1 from the current environment data1 lt-edit(dataframe()) Starts empty GUI spreadsheet editor for manual data entry
Basic Usage of R
class(object)displays the object type
str(object)displays the internal type and structure of an R objectgtstr(m)num [14 13] 0248 0589 -0589 0504 1524
attributes(object)Returns an objects attribute listgt attributes(m)$dim[1] 4 3
dir() Reads content of current working directory
getwd() Returns current working directory
setwd(homeuser) Changes current working directory to user specified directory
Data Import
readdelim(clipboard header=T) Command to copyamppastetables from Excel or other programs into R If the header argument isset to FALSE then the first line of the data set will not be used as column titlesscan(my_file) reads vectorarray into vector from file or keyboardmy_framelt-readdelim(ldquocAffymetrixaffy1txt nastrings= fill=TRUE header=T sep=t) The function readdelim() is often more flexible for importing tables with empty fields and long character strings (eg gene descriptions)
It supports data import on the webDifferent coding of missing values (nastrings=ldquoNArdquoor ldquordquo)
Data columns can be separated by TAB comma or semicolon (sep=ldquordquo)
Data Import
There are some alternatives for reading data as followingsmy_framelt-readtable(file=ldquopath header=TRUE sep=t)Reads in table with info on column headers and field separators datalt-readtable(httpwwwcmuedutwexampletxt header=TRUE)
my_framelt-readcsv(file=ldquopathldquo header=TRUE) reads csvfile with comma separated value You can skip lines read a limited number of lines different decimal separator and more importing options The foreign package can read files from Stata SAS and SPSS
Data Export
writetable(iris clipboard sep=t colnames=NA quote=F) Command to copyamppastefrom R into Excel or other programs It writes the data of an R data frame object into the clipbroardfrom where it can be pasted into other applicationswritetable(dataframe file=ldquofile path sep=t colnames= NA) Writes data frame to a tab-delimited text file The argument colnames= NA makes sure that the titles align with columns when rowindex names are exported (default)write(x file=file path) Writes matrix data to a file
Basic Operators
Comparison operators equal == not equal = greaterless than gt lt greaterless than or equal gt= lt= Example 1 == 1 Returns TRUE
1048710Logical operators ANDamp
x lt- 110 y lt- 101 Creates the sample vectors x and y x gt y amp x gt 5 Returns TRUE where both comparisons return TRUE OR |
x == y | x = y Returns TRUE where at least one comparison returns TRUE NOT
x gt y The sign returns the negation (opposite) of a logical vector
Basic Operators
Calculations Four basic arithmetic functions addition subtraction multiplication and division 1 + 1 1 - 1 1 1 1 1 Returns the results of these calculations
Calculations on vectors x lt- 120 sum(x) mean(x) sd(x) sqrt(x) rank(x)sort(x) Calculates for the vector x its sum mean standard deviation and square root etc x lt- 120 y lt- 120 x + y Calculates the sum for each element in the vectors x and y
Data Types
Numeric data 1 2 3 x lt- c(1 2 3) x isnumeric(x) ascharacter(x) Creates a numeric vector checks for the data type and converts it into a character vectorCharacter data a b c x lt- c(1 2 3) x ischaracter(x) asnumeric(x)
Creates a character vector checks for the data type and converts it into a numeric vectorLogical data TRUE FALSE TRUE 110 lt 5 Returns TRUE where x is lt 5
Object Types
vectors ordered collection of numeric character complex and logical values factors special type vectors with grouping information of its components data frames two dimensional structures with different data types matrices two dimensional structures with data of same type arrays multidimensional arrays of vectors lists general form of vectors with different types of elements functions piece of code
Subsetting Syntax
my_object[index] Subsettingof one dimensional objects like vectors and factors Returns elements with positions in indexmy_object[rowindex colindex] Subsettingof two dimensional objects like matrices and data framesmy_object[rowindex colindex dim] Subsettingof three dimensional objects like arraysdim(my_object) Returns the numbers of row and columnmy_logicallt-(my_objectgt 10) Generates a logical vector as examplemy_object[my_logical] Returns the elements where my_logical contains TRUE valuesmy_object$Name1 Returns the lsquoName1 column in the my_objectdata frame
Vector amp List
bull vector an ordered collection of data of the same type gt a = c(751)gt a[2][1] 5
bull list an ordered collection of data of arbitrary types gt doe = list(name=johnage=28married=F)gt doe$name[1] johnldquogt doe$age[1] 28bull Typically vector elements are accessed by their index (an integer)
list elements by their name (a character string) But both types support both access methods
Programming in R
Ifelse StatementExample x lt- 110 ifelse(xlt5 | xgt8 x 0)
For Loop Example mean mydf lt- irismyve lt- NULLfor(i in 1length(mydf[1])) myve lt- c(myve mean(asvector(asmatrix(mydf[i1
3]))))
While Loop Example z lt- 0while(zlt5) z lt- z+2print(z)
Writing your own functionsgt f=function(x)x^2+2xgtf(3)[1] 15
Graphics
library(UsingR)scatterwithhist(faithful$eruptionsfaithf ul$waiting)
Reference
2007 年 R 統計軟體研習會 ------- 蔡政安 教授httpwwwstateduntuedutw2007conferenceindexhtm
An Introduction to R
httpcranr-projectorgdocmanualsR-intropdf
~The End~
Thanks for your listening
How it makeamp where we get it
R is available in several forms the sources written mainly in C (and some routines in Fortran) essentially for Unix and Linux machines or some pre-compiled binaries for Windows Linux (Debian Mandrake RedHat SuSe) Macintosh and Alpha UnixThe files needed to install R either from the sources or from the pre-compiled binaries are distributed from the internet site of the Comprehensive R Archive Network (CRAN) where the instructions for the installation are also available httpCRANR-projectorg
R has many functions for statistical analyses and graphics the latter are visualized immediately in their own window and can be saved in various formats (jpg png bmp ps pdf emf pictex xfig) The results from a statistical analysis are displayed on the screen some intermediate results (P-values regression coefficients residuals ) can be saved written in a file or used in subsequent analyses
There is an important difference between S (and hence R) and the other main statistical systems In S a statistical analysis is normally done as a series of steps with intermediate results being stored in objectsSAS and SPSS will give copious output from a regression or discriminant analysis R will give minimal output and store the results in a t object for subsequent interrogation by further R functions
Disadvantages of R
R is not efficient in handling large data sets
Slow computation for a large number of do-loops compared to CC++ Fortran etc
Self-Learning is not so convenient compared to ldquopoint-and-clickrdquo statistics software
No warrantee and informal support
Needed to upgrade R version to install some newly developed packages
Getting Help in R
library()lists all available libraries on system
help(command)getting help for one command eg help(heatmap)
helpsearch(ldquotopicrdquo)searches help system for packages associated with the topic eg helpsearch(ldquodistributionrdquo)
helpstart()starts local HTML interface
q()quits R console
Basic Usage of R
The general R command syntax uses the assignment operator ldquolt-rdquo(or ldquo=ldquo) to assign data to objectobject lt- function (arguments)
source(ldquomyscriptRrdquo) command to execute an R script named as myscriptRobjects()or ls() list the names of all objects rm(data1) Remove the object named data1 from the current environment data1 lt-edit(dataframe()) Starts empty GUI spreadsheet editor for manual data entry
Basic Usage of R
class(object)displays the object type
str(object)displays the internal type and structure of an R objectgtstr(m)num [14 13] 0248 0589 -0589 0504 1524
attributes(object)Returns an objects attribute listgt attributes(m)$dim[1] 4 3
dir() Reads content of current working directory
getwd() Returns current working directory
setwd(homeuser) Changes current working directory to user specified directory
Data Import
readdelim(clipboard header=T) Command to copyamppastetables from Excel or other programs into R If the header argument isset to FALSE then the first line of the data set will not be used as column titlesscan(my_file) reads vectorarray into vector from file or keyboardmy_framelt-readdelim(ldquocAffymetrixaffy1txt nastrings= fill=TRUE header=T sep=t) The function readdelim() is often more flexible for importing tables with empty fields and long character strings (eg gene descriptions)
It supports data import on the webDifferent coding of missing values (nastrings=ldquoNArdquoor ldquordquo)
Data columns can be separated by TAB comma or semicolon (sep=ldquordquo)
Data Import
There are some alternatives for reading data as followingsmy_framelt-readtable(file=ldquopath header=TRUE sep=t)Reads in table with info on column headers and field separators datalt-readtable(httpwwwcmuedutwexampletxt header=TRUE)
my_framelt-readcsv(file=ldquopathldquo header=TRUE) reads csvfile with comma separated value You can skip lines read a limited number of lines different decimal separator and more importing options The foreign package can read files from Stata SAS and SPSS
Data Export
writetable(iris clipboard sep=t colnames=NA quote=F) Command to copyamppastefrom R into Excel or other programs It writes the data of an R data frame object into the clipbroardfrom where it can be pasted into other applicationswritetable(dataframe file=ldquofile path sep=t colnames= NA) Writes data frame to a tab-delimited text file The argument colnames= NA makes sure that the titles align with columns when rowindex names are exported (default)write(x file=file path) Writes matrix data to a file
Basic Operators
Comparison operators equal == not equal = greaterless than gt lt greaterless than or equal gt= lt= Example 1 == 1 Returns TRUE
1048710Logical operators ANDamp
x lt- 110 y lt- 101 Creates the sample vectors x and y x gt y amp x gt 5 Returns TRUE where both comparisons return TRUE OR |
x == y | x = y Returns TRUE where at least one comparison returns TRUE NOT
x gt y The sign returns the negation (opposite) of a logical vector
Basic Operators
Calculations Four basic arithmetic functions addition subtraction multiplication and division 1 + 1 1 - 1 1 1 1 1 Returns the results of these calculations
Calculations on vectors x lt- 120 sum(x) mean(x) sd(x) sqrt(x) rank(x)sort(x) Calculates for the vector x its sum mean standard deviation and square root etc x lt- 120 y lt- 120 x + y Calculates the sum for each element in the vectors x and y
Data Types
Numeric data 1 2 3 x lt- c(1 2 3) x isnumeric(x) ascharacter(x) Creates a numeric vector checks for the data type and converts it into a character vectorCharacter data a b c x lt- c(1 2 3) x ischaracter(x) asnumeric(x)
Creates a character vector checks for the data type and converts it into a numeric vectorLogical data TRUE FALSE TRUE 110 lt 5 Returns TRUE where x is lt 5
Object Types
vectors ordered collection of numeric character complex and logical values factors special type vectors with grouping information of its components data frames two dimensional structures with different data types matrices two dimensional structures with data of same type arrays multidimensional arrays of vectors lists general form of vectors with different types of elements functions piece of code
Subsetting Syntax
my_object[index] Subsettingof one dimensional objects like vectors and factors Returns elements with positions in indexmy_object[rowindex colindex] Subsettingof two dimensional objects like matrices and data framesmy_object[rowindex colindex dim] Subsettingof three dimensional objects like arraysdim(my_object) Returns the numbers of row and columnmy_logicallt-(my_objectgt 10) Generates a logical vector as examplemy_object[my_logical] Returns the elements where my_logical contains TRUE valuesmy_object$Name1 Returns the lsquoName1 column in the my_objectdata frame
Vector amp List
bull vector an ordered collection of data of the same type gt a = c(751)gt a[2][1] 5
bull list an ordered collection of data of arbitrary types gt doe = list(name=johnage=28married=F)gt doe$name[1] johnldquogt doe$age[1] 28bull Typically vector elements are accessed by their index (an integer)
list elements by their name (a character string) But both types support both access methods
Programming in R
Ifelse StatementExample x lt- 110 ifelse(xlt5 | xgt8 x 0)
For Loop Example mean mydf lt- irismyve lt- NULLfor(i in 1length(mydf[1])) myve lt- c(myve mean(asvector(asmatrix(mydf[i1
3]))))
While Loop Example z lt- 0while(zlt5) z lt- z+2print(z)
Writing your own functionsgt f=function(x)x^2+2xgtf(3)[1] 15
Graphics
library(UsingR)scatterwithhist(faithful$eruptionsfaithf ul$waiting)
Reference
2007 年 R 統計軟體研習會 ------- 蔡政安 教授httpwwwstateduntuedutw2007conferenceindexhtm
An Introduction to R
httpcranr-projectorgdocmanualsR-intropdf
~The End~
Thanks for your listening
R has many functions for statistical analyses and graphics the latter are visualized immediately in their own window and can be saved in various formats (jpg png bmp ps pdf emf pictex xfig) The results from a statistical analysis are displayed on the screen some intermediate results (P-values regression coefficients residuals ) can be saved written in a file or used in subsequent analyses
There is an important difference between S (and hence R) and the other main statistical systems In S a statistical analysis is normally done as a series of steps with intermediate results being stored in objectsSAS and SPSS will give copious output from a regression or discriminant analysis R will give minimal output and store the results in a t object for subsequent interrogation by further R functions
Disadvantages of R
R is not efficient in handling large data sets
Slow computation for a large number of do-loops compared to CC++ Fortran etc
Self-Learning is not so convenient compared to ldquopoint-and-clickrdquo statistics software
No warrantee and informal support
Needed to upgrade R version to install some newly developed packages
Getting Help in R
library()lists all available libraries on system
help(command)getting help for one command eg help(heatmap)
helpsearch(ldquotopicrdquo)searches help system for packages associated with the topic eg helpsearch(ldquodistributionrdquo)
helpstart()starts local HTML interface
q()quits R console
Basic Usage of R
The general R command syntax uses the assignment operator ldquolt-rdquo(or ldquo=ldquo) to assign data to objectobject lt- function (arguments)
source(ldquomyscriptRrdquo) command to execute an R script named as myscriptRobjects()or ls() list the names of all objects rm(data1) Remove the object named data1 from the current environment data1 lt-edit(dataframe()) Starts empty GUI spreadsheet editor for manual data entry
Basic Usage of R
class(object)displays the object type
str(object)displays the internal type and structure of an R objectgtstr(m)num [14 13] 0248 0589 -0589 0504 1524
attributes(object)Returns an objects attribute listgt attributes(m)$dim[1] 4 3
dir() Reads content of current working directory
getwd() Returns current working directory
setwd(homeuser) Changes current working directory to user specified directory
Data Import
readdelim(clipboard header=T) Command to copyamppastetables from Excel or other programs into R If the header argument isset to FALSE then the first line of the data set will not be used as column titlesscan(my_file) reads vectorarray into vector from file or keyboardmy_framelt-readdelim(ldquocAffymetrixaffy1txt nastrings= fill=TRUE header=T sep=t) The function readdelim() is often more flexible for importing tables with empty fields and long character strings (eg gene descriptions)
It supports data import on the webDifferent coding of missing values (nastrings=ldquoNArdquoor ldquordquo)
Data columns can be separated by TAB comma or semicolon (sep=ldquordquo)
Data Import
There are some alternatives for reading data as followingsmy_framelt-readtable(file=ldquopath header=TRUE sep=t)Reads in table with info on column headers and field separators datalt-readtable(httpwwwcmuedutwexampletxt header=TRUE)
my_framelt-readcsv(file=ldquopathldquo header=TRUE) reads csvfile with comma separated value You can skip lines read a limited number of lines different decimal separator and more importing options The foreign package can read files from Stata SAS and SPSS
Data Export
writetable(iris clipboard sep=t colnames=NA quote=F) Command to copyamppastefrom R into Excel or other programs It writes the data of an R data frame object into the clipbroardfrom where it can be pasted into other applicationswritetable(dataframe file=ldquofile path sep=t colnames= NA) Writes data frame to a tab-delimited text file The argument colnames= NA makes sure that the titles align with columns when rowindex names are exported (default)write(x file=file path) Writes matrix data to a file
Basic Operators
Comparison operators equal == not equal = greaterless than gt lt greaterless than or equal gt= lt= Example 1 == 1 Returns TRUE
1048710Logical operators ANDamp
x lt- 110 y lt- 101 Creates the sample vectors x and y x gt y amp x gt 5 Returns TRUE where both comparisons return TRUE OR |
x == y | x = y Returns TRUE where at least one comparison returns TRUE NOT
x gt y The sign returns the negation (opposite) of a logical vector
Basic Operators
Calculations Four basic arithmetic functions addition subtraction multiplication and division 1 + 1 1 - 1 1 1 1 1 Returns the results of these calculations
Calculations on vectors x lt- 120 sum(x) mean(x) sd(x) sqrt(x) rank(x)sort(x) Calculates for the vector x its sum mean standard deviation and square root etc x lt- 120 y lt- 120 x + y Calculates the sum for each element in the vectors x and y
Data Types
Numeric data 1 2 3 x lt- c(1 2 3) x isnumeric(x) ascharacter(x) Creates a numeric vector checks for the data type and converts it into a character vectorCharacter data a b c x lt- c(1 2 3) x ischaracter(x) asnumeric(x)
Creates a character vector checks for the data type and converts it into a numeric vectorLogical data TRUE FALSE TRUE 110 lt 5 Returns TRUE where x is lt 5
Object Types
vectors ordered collection of numeric character complex and logical values factors special type vectors with grouping information of its components data frames two dimensional structures with different data types matrices two dimensional structures with data of same type arrays multidimensional arrays of vectors lists general form of vectors with different types of elements functions piece of code
Subsetting Syntax
my_object[index] Subsettingof one dimensional objects like vectors and factors Returns elements with positions in indexmy_object[rowindex colindex] Subsettingof two dimensional objects like matrices and data framesmy_object[rowindex colindex dim] Subsettingof three dimensional objects like arraysdim(my_object) Returns the numbers of row and columnmy_logicallt-(my_objectgt 10) Generates a logical vector as examplemy_object[my_logical] Returns the elements where my_logical contains TRUE valuesmy_object$Name1 Returns the lsquoName1 column in the my_objectdata frame
Vector amp List
bull vector an ordered collection of data of the same type gt a = c(751)gt a[2][1] 5
bull list an ordered collection of data of arbitrary types gt doe = list(name=johnage=28married=F)gt doe$name[1] johnldquogt doe$age[1] 28bull Typically vector elements are accessed by their index (an integer)
list elements by their name (a character string) But both types support both access methods
Programming in R
Ifelse StatementExample x lt- 110 ifelse(xlt5 | xgt8 x 0)
For Loop Example mean mydf lt- irismyve lt- NULLfor(i in 1length(mydf[1])) myve lt- c(myve mean(asvector(asmatrix(mydf[i1
3]))))
While Loop Example z lt- 0while(zlt5) z lt- z+2print(z)
Writing your own functionsgt f=function(x)x^2+2xgtf(3)[1] 15
Graphics
library(UsingR)scatterwithhist(faithful$eruptionsfaithf ul$waiting)
Reference
2007 年 R 統計軟體研習會 ------- 蔡政安 教授httpwwwstateduntuedutw2007conferenceindexhtm
An Introduction to R
httpcranr-projectorgdocmanualsR-intropdf
~The End~
Thanks for your listening
There is an important difference between S (and hence R) and the other main statistical systems In S a statistical analysis is normally done as a series of steps with intermediate results being stored in objectsSAS and SPSS will give copious output from a regression or discriminant analysis R will give minimal output and store the results in a t object for subsequent interrogation by further R functions
Disadvantages of R
R is not efficient in handling large data sets
Slow computation for a large number of do-loops compared to CC++ Fortran etc
Self-Learning is not so convenient compared to ldquopoint-and-clickrdquo statistics software
No warrantee and informal support
Needed to upgrade R version to install some newly developed packages
Getting Help in R
library()lists all available libraries on system
help(command)getting help for one command eg help(heatmap)
helpsearch(ldquotopicrdquo)searches help system for packages associated with the topic eg helpsearch(ldquodistributionrdquo)
helpstart()starts local HTML interface
q()quits R console
Basic Usage of R
The general R command syntax uses the assignment operator ldquolt-rdquo(or ldquo=ldquo) to assign data to objectobject lt- function (arguments)
source(ldquomyscriptRrdquo) command to execute an R script named as myscriptRobjects()or ls() list the names of all objects rm(data1) Remove the object named data1 from the current environment data1 lt-edit(dataframe()) Starts empty GUI spreadsheet editor for manual data entry
Basic Usage of R
class(object)displays the object type
str(object)displays the internal type and structure of an R objectgtstr(m)num [14 13] 0248 0589 -0589 0504 1524
attributes(object)Returns an objects attribute listgt attributes(m)$dim[1] 4 3
dir() Reads content of current working directory
getwd() Returns current working directory
setwd(homeuser) Changes current working directory to user specified directory
Data Import
readdelim(clipboard header=T) Command to copyamppastetables from Excel or other programs into R If the header argument isset to FALSE then the first line of the data set will not be used as column titlesscan(my_file) reads vectorarray into vector from file or keyboardmy_framelt-readdelim(ldquocAffymetrixaffy1txt nastrings= fill=TRUE header=T sep=t) The function readdelim() is often more flexible for importing tables with empty fields and long character strings (eg gene descriptions)
It supports data import on the webDifferent coding of missing values (nastrings=ldquoNArdquoor ldquordquo)
Data columns can be separated by TAB comma or semicolon (sep=ldquordquo)
Data Import
There are some alternatives for reading data as followingsmy_framelt-readtable(file=ldquopath header=TRUE sep=t)Reads in table with info on column headers and field separators datalt-readtable(httpwwwcmuedutwexampletxt header=TRUE)
my_framelt-readcsv(file=ldquopathldquo header=TRUE) reads csvfile with comma separated value You can skip lines read a limited number of lines different decimal separator and more importing options The foreign package can read files from Stata SAS and SPSS
Data Export
writetable(iris clipboard sep=t colnames=NA quote=F) Command to copyamppastefrom R into Excel or other programs It writes the data of an R data frame object into the clipbroardfrom where it can be pasted into other applicationswritetable(dataframe file=ldquofile path sep=t colnames= NA) Writes data frame to a tab-delimited text file The argument colnames= NA makes sure that the titles align with columns when rowindex names are exported (default)write(x file=file path) Writes matrix data to a file
Basic Operators
Comparison operators equal == not equal = greaterless than gt lt greaterless than or equal gt= lt= Example 1 == 1 Returns TRUE
1048710Logical operators ANDamp
x lt- 110 y lt- 101 Creates the sample vectors x and y x gt y amp x gt 5 Returns TRUE where both comparisons return TRUE OR |
x == y | x = y Returns TRUE where at least one comparison returns TRUE NOT
x gt y The sign returns the negation (opposite) of a logical vector
Basic Operators
Calculations Four basic arithmetic functions addition subtraction multiplication and division 1 + 1 1 - 1 1 1 1 1 Returns the results of these calculations
Calculations on vectors x lt- 120 sum(x) mean(x) sd(x) sqrt(x) rank(x)sort(x) Calculates for the vector x its sum mean standard deviation and square root etc x lt- 120 y lt- 120 x + y Calculates the sum for each element in the vectors x and y
Data Types
Numeric data 1 2 3 x lt- c(1 2 3) x isnumeric(x) ascharacter(x) Creates a numeric vector checks for the data type and converts it into a character vectorCharacter data a b c x lt- c(1 2 3) x ischaracter(x) asnumeric(x)
Creates a character vector checks for the data type and converts it into a numeric vectorLogical data TRUE FALSE TRUE 110 lt 5 Returns TRUE where x is lt 5
Object Types
vectors ordered collection of numeric character complex and logical values factors special type vectors with grouping information of its components data frames two dimensional structures with different data types matrices two dimensional structures with data of same type arrays multidimensional arrays of vectors lists general form of vectors with different types of elements functions piece of code
Subsetting Syntax
my_object[index] Subsettingof one dimensional objects like vectors and factors Returns elements with positions in indexmy_object[rowindex colindex] Subsettingof two dimensional objects like matrices and data framesmy_object[rowindex colindex dim] Subsettingof three dimensional objects like arraysdim(my_object) Returns the numbers of row and columnmy_logicallt-(my_objectgt 10) Generates a logical vector as examplemy_object[my_logical] Returns the elements where my_logical contains TRUE valuesmy_object$Name1 Returns the lsquoName1 column in the my_objectdata frame
Vector amp List
bull vector an ordered collection of data of the same type gt a = c(751)gt a[2][1] 5
bull list an ordered collection of data of arbitrary types gt doe = list(name=johnage=28married=F)gt doe$name[1] johnldquogt doe$age[1] 28bull Typically vector elements are accessed by their index (an integer)
list elements by their name (a character string) But both types support both access methods
Programming in R
Ifelse StatementExample x lt- 110 ifelse(xlt5 | xgt8 x 0)
For Loop Example mean mydf lt- irismyve lt- NULLfor(i in 1length(mydf[1])) myve lt- c(myve mean(asvector(asmatrix(mydf[i1
3]))))
While Loop Example z lt- 0while(zlt5) z lt- z+2print(z)
Writing your own functionsgt f=function(x)x^2+2xgtf(3)[1] 15
Graphics
library(UsingR)scatterwithhist(faithful$eruptionsfaithf ul$waiting)
Reference
2007 年 R 統計軟體研習會 ------- 蔡政安 教授httpwwwstateduntuedutw2007conferenceindexhtm
An Introduction to R
httpcranr-projectorgdocmanualsR-intropdf
~The End~
Thanks for your listening
Disadvantages of R
R is not efficient in handling large data sets
Slow computation for a large number of do-loops compared to CC++ Fortran etc
Self-Learning is not so convenient compared to ldquopoint-and-clickrdquo statistics software
No warrantee and informal support
Needed to upgrade R version to install some newly developed packages
Getting Help in R
library()lists all available libraries on system
help(command)getting help for one command eg help(heatmap)
helpsearch(ldquotopicrdquo)searches help system for packages associated with the topic eg helpsearch(ldquodistributionrdquo)
helpstart()starts local HTML interface
q()quits R console
Basic Usage of R
The general R command syntax uses the assignment operator ldquolt-rdquo(or ldquo=ldquo) to assign data to objectobject lt- function (arguments)
source(ldquomyscriptRrdquo) command to execute an R script named as myscriptRobjects()or ls() list the names of all objects rm(data1) Remove the object named data1 from the current environment data1 lt-edit(dataframe()) Starts empty GUI spreadsheet editor for manual data entry
Basic Usage of R
class(object)displays the object type
str(object)displays the internal type and structure of an R objectgtstr(m)num [14 13] 0248 0589 -0589 0504 1524
attributes(object)Returns an objects attribute listgt attributes(m)$dim[1] 4 3
dir() Reads content of current working directory
getwd() Returns current working directory
setwd(homeuser) Changes current working directory to user specified directory
Data Import
readdelim(clipboard header=T) Command to copyamppastetables from Excel or other programs into R If the header argument isset to FALSE then the first line of the data set will not be used as column titlesscan(my_file) reads vectorarray into vector from file or keyboardmy_framelt-readdelim(ldquocAffymetrixaffy1txt nastrings= fill=TRUE header=T sep=t) The function readdelim() is often more flexible for importing tables with empty fields and long character strings (eg gene descriptions)
It supports data import on the webDifferent coding of missing values (nastrings=ldquoNArdquoor ldquordquo)
Data columns can be separated by TAB comma or semicolon (sep=ldquordquo)
Data Import
There are some alternatives for reading data as followingsmy_framelt-readtable(file=ldquopath header=TRUE sep=t)Reads in table with info on column headers and field separators datalt-readtable(httpwwwcmuedutwexampletxt header=TRUE)
my_framelt-readcsv(file=ldquopathldquo header=TRUE) reads csvfile with comma separated value You can skip lines read a limited number of lines different decimal separator and more importing options The foreign package can read files from Stata SAS and SPSS
Data Export
writetable(iris clipboard sep=t colnames=NA quote=F) Command to copyamppastefrom R into Excel or other programs It writes the data of an R data frame object into the clipbroardfrom where it can be pasted into other applicationswritetable(dataframe file=ldquofile path sep=t colnames= NA) Writes data frame to a tab-delimited text file The argument colnames= NA makes sure that the titles align with columns when rowindex names are exported (default)write(x file=file path) Writes matrix data to a file
Basic Operators
Comparison operators equal == not equal = greaterless than gt lt greaterless than or equal gt= lt= Example 1 == 1 Returns TRUE
1048710Logical operators ANDamp
x lt- 110 y lt- 101 Creates the sample vectors x and y x gt y amp x gt 5 Returns TRUE where both comparisons return TRUE OR |
x == y | x = y Returns TRUE where at least one comparison returns TRUE NOT
x gt y The sign returns the negation (opposite) of a logical vector
Basic Operators
Calculations Four basic arithmetic functions addition subtraction multiplication and division 1 + 1 1 - 1 1 1 1 1 Returns the results of these calculations
Calculations on vectors x lt- 120 sum(x) mean(x) sd(x) sqrt(x) rank(x)sort(x) Calculates for the vector x its sum mean standard deviation and square root etc x lt- 120 y lt- 120 x + y Calculates the sum for each element in the vectors x and y
Data Types
Numeric data 1 2 3 x lt- c(1 2 3) x isnumeric(x) ascharacter(x) Creates a numeric vector checks for the data type and converts it into a character vectorCharacter data a b c x lt- c(1 2 3) x ischaracter(x) asnumeric(x)
Creates a character vector checks for the data type and converts it into a numeric vectorLogical data TRUE FALSE TRUE 110 lt 5 Returns TRUE where x is lt 5
Object Types
vectors ordered collection of numeric character complex and logical values factors special type vectors with grouping information of its components data frames two dimensional structures with different data types matrices two dimensional structures with data of same type arrays multidimensional arrays of vectors lists general form of vectors with different types of elements functions piece of code
Subsetting Syntax
my_object[index] Subsettingof one dimensional objects like vectors and factors Returns elements with positions in indexmy_object[rowindex colindex] Subsettingof two dimensional objects like matrices and data framesmy_object[rowindex colindex dim] Subsettingof three dimensional objects like arraysdim(my_object) Returns the numbers of row and columnmy_logicallt-(my_objectgt 10) Generates a logical vector as examplemy_object[my_logical] Returns the elements where my_logical contains TRUE valuesmy_object$Name1 Returns the lsquoName1 column in the my_objectdata frame
Vector amp List
bull vector an ordered collection of data of the same type gt a = c(751)gt a[2][1] 5
bull list an ordered collection of data of arbitrary types gt doe = list(name=johnage=28married=F)gt doe$name[1] johnldquogt doe$age[1] 28bull Typically vector elements are accessed by their index (an integer)
list elements by their name (a character string) But both types support both access methods
Programming in R
Ifelse StatementExample x lt- 110 ifelse(xlt5 | xgt8 x 0)
For Loop Example mean mydf lt- irismyve lt- NULLfor(i in 1length(mydf[1])) myve lt- c(myve mean(asvector(asmatrix(mydf[i1
3]))))
While Loop Example z lt- 0while(zlt5) z lt- z+2print(z)
Writing your own functionsgt f=function(x)x^2+2xgtf(3)[1] 15
Graphics
library(UsingR)scatterwithhist(faithful$eruptionsfaithf ul$waiting)
Reference
2007 年 R 統計軟體研習會 ------- 蔡政安 教授httpwwwstateduntuedutw2007conferenceindexhtm
An Introduction to R
httpcranr-projectorgdocmanualsR-intropdf
~The End~
Thanks for your listening
Getting Help in R
library()lists all available libraries on system
help(command)getting help for one command eg help(heatmap)
helpsearch(ldquotopicrdquo)searches help system for packages associated with the topic eg helpsearch(ldquodistributionrdquo)
helpstart()starts local HTML interface
q()quits R console
Basic Usage of R
The general R command syntax uses the assignment operator ldquolt-rdquo(or ldquo=ldquo) to assign data to objectobject lt- function (arguments)
source(ldquomyscriptRrdquo) command to execute an R script named as myscriptRobjects()or ls() list the names of all objects rm(data1) Remove the object named data1 from the current environment data1 lt-edit(dataframe()) Starts empty GUI spreadsheet editor for manual data entry
Basic Usage of R
class(object)displays the object type
str(object)displays the internal type and structure of an R objectgtstr(m)num [14 13] 0248 0589 -0589 0504 1524
attributes(object)Returns an objects attribute listgt attributes(m)$dim[1] 4 3
dir() Reads content of current working directory
getwd() Returns current working directory
setwd(homeuser) Changes current working directory to user specified directory
Data Import
readdelim(clipboard header=T) Command to copyamppastetables from Excel or other programs into R If the header argument isset to FALSE then the first line of the data set will not be used as column titlesscan(my_file) reads vectorarray into vector from file or keyboardmy_framelt-readdelim(ldquocAffymetrixaffy1txt nastrings= fill=TRUE header=T sep=t) The function readdelim() is often more flexible for importing tables with empty fields and long character strings (eg gene descriptions)
It supports data import on the webDifferent coding of missing values (nastrings=ldquoNArdquoor ldquordquo)
Data columns can be separated by TAB comma or semicolon (sep=ldquordquo)
Data Import
There are some alternatives for reading data as followingsmy_framelt-readtable(file=ldquopath header=TRUE sep=t)Reads in table with info on column headers and field separators datalt-readtable(httpwwwcmuedutwexampletxt header=TRUE)
my_framelt-readcsv(file=ldquopathldquo header=TRUE) reads csvfile with comma separated value You can skip lines read a limited number of lines different decimal separator and more importing options The foreign package can read files from Stata SAS and SPSS
Data Export
writetable(iris clipboard sep=t colnames=NA quote=F) Command to copyamppastefrom R into Excel or other programs It writes the data of an R data frame object into the clipbroardfrom where it can be pasted into other applicationswritetable(dataframe file=ldquofile path sep=t colnames= NA) Writes data frame to a tab-delimited text file The argument colnames= NA makes sure that the titles align with columns when rowindex names are exported (default)write(x file=file path) Writes matrix data to a file
Basic Operators
Comparison operators equal == not equal = greaterless than gt lt greaterless than or equal gt= lt= Example 1 == 1 Returns TRUE
1048710Logical operators ANDamp
x lt- 110 y lt- 101 Creates the sample vectors x and y x gt y amp x gt 5 Returns TRUE where both comparisons return TRUE OR |
x == y | x = y Returns TRUE where at least one comparison returns TRUE NOT
x gt y The sign returns the negation (opposite) of a logical vector
Basic Operators
Calculations Four basic arithmetic functions addition subtraction multiplication and division 1 + 1 1 - 1 1 1 1 1 Returns the results of these calculations
Calculations on vectors x lt- 120 sum(x) mean(x) sd(x) sqrt(x) rank(x)sort(x) Calculates for the vector x its sum mean standard deviation and square root etc x lt- 120 y lt- 120 x + y Calculates the sum for each element in the vectors x and y
Data Types
Numeric data 1 2 3 x lt- c(1 2 3) x isnumeric(x) ascharacter(x) Creates a numeric vector checks for the data type and converts it into a character vectorCharacter data a b c x lt- c(1 2 3) x ischaracter(x) asnumeric(x)
Creates a character vector checks for the data type and converts it into a numeric vectorLogical data TRUE FALSE TRUE 110 lt 5 Returns TRUE where x is lt 5
Object Types
vectors ordered collection of numeric character complex and logical values factors special type vectors with grouping information of its components data frames two dimensional structures with different data types matrices two dimensional structures with data of same type arrays multidimensional arrays of vectors lists general form of vectors with different types of elements functions piece of code
Subsetting Syntax
my_object[index] Subsettingof one dimensional objects like vectors and factors Returns elements with positions in indexmy_object[rowindex colindex] Subsettingof two dimensional objects like matrices and data framesmy_object[rowindex colindex dim] Subsettingof three dimensional objects like arraysdim(my_object) Returns the numbers of row and columnmy_logicallt-(my_objectgt 10) Generates a logical vector as examplemy_object[my_logical] Returns the elements where my_logical contains TRUE valuesmy_object$Name1 Returns the lsquoName1 column in the my_objectdata frame
Vector amp List
bull vector an ordered collection of data of the same type gt a = c(751)gt a[2][1] 5
bull list an ordered collection of data of arbitrary types gt doe = list(name=johnage=28married=F)gt doe$name[1] johnldquogt doe$age[1] 28bull Typically vector elements are accessed by their index (an integer)
list elements by their name (a character string) But both types support both access methods
Programming in R
Ifelse StatementExample x lt- 110 ifelse(xlt5 | xgt8 x 0)
For Loop Example mean mydf lt- irismyve lt- NULLfor(i in 1length(mydf[1])) myve lt- c(myve mean(asvector(asmatrix(mydf[i1
3]))))
While Loop Example z lt- 0while(zlt5) z lt- z+2print(z)
Writing your own functionsgt f=function(x)x^2+2xgtf(3)[1] 15
Graphics
library(UsingR)scatterwithhist(faithful$eruptionsfaithf ul$waiting)
Reference
2007 年 R 統計軟體研習會 ------- 蔡政安 教授httpwwwstateduntuedutw2007conferenceindexhtm
An Introduction to R
httpcranr-projectorgdocmanualsR-intropdf
~The End~
Thanks for your listening
Basic Usage of R
The general R command syntax uses the assignment operator ldquolt-rdquo(or ldquo=ldquo) to assign data to objectobject lt- function (arguments)
source(ldquomyscriptRrdquo) command to execute an R script named as myscriptRobjects()or ls() list the names of all objects rm(data1) Remove the object named data1 from the current environment data1 lt-edit(dataframe()) Starts empty GUI spreadsheet editor for manual data entry
Basic Usage of R
class(object)displays the object type
str(object)displays the internal type and structure of an R objectgtstr(m)num [14 13] 0248 0589 -0589 0504 1524
attributes(object)Returns an objects attribute listgt attributes(m)$dim[1] 4 3
dir() Reads content of current working directory
getwd() Returns current working directory
setwd(homeuser) Changes current working directory to user specified directory
Data Import
readdelim(clipboard header=T) Command to copyamppastetables from Excel or other programs into R If the header argument isset to FALSE then the first line of the data set will not be used as column titlesscan(my_file) reads vectorarray into vector from file or keyboardmy_framelt-readdelim(ldquocAffymetrixaffy1txt nastrings= fill=TRUE header=T sep=t) The function readdelim() is often more flexible for importing tables with empty fields and long character strings (eg gene descriptions)
It supports data import on the webDifferent coding of missing values (nastrings=ldquoNArdquoor ldquordquo)
Data columns can be separated by TAB comma or semicolon (sep=ldquordquo)
Data Import
There are some alternatives for reading data as followingsmy_framelt-readtable(file=ldquopath header=TRUE sep=t)Reads in table with info on column headers and field separators datalt-readtable(httpwwwcmuedutwexampletxt header=TRUE)
my_framelt-readcsv(file=ldquopathldquo header=TRUE) reads csvfile with comma separated value You can skip lines read a limited number of lines different decimal separator and more importing options The foreign package can read files from Stata SAS and SPSS
Data Export
writetable(iris clipboard sep=t colnames=NA quote=F) Command to copyamppastefrom R into Excel or other programs It writes the data of an R data frame object into the clipbroardfrom where it can be pasted into other applicationswritetable(dataframe file=ldquofile path sep=t colnames= NA) Writes data frame to a tab-delimited text file The argument colnames= NA makes sure that the titles align with columns when rowindex names are exported (default)write(x file=file path) Writes matrix data to a file
Basic Operators
Comparison operators equal == not equal = greaterless than gt lt greaterless than or equal gt= lt= Example 1 == 1 Returns TRUE
1048710Logical operators ANDamp
x lt- 110 y lt- 101 Creates the sample vectors x and y x gt y amp x gt 5 Returns TRUE where both comparisons return TRUE OR |
x == y | x = y Returns TRUE where at least one comparison returns TRUE NOT
x gt y The sign returns the negation (opposite) of a logical vector
Basic Operators
Calculations Four basic arithmetic functions addition subtraction multiplication and division 1 + 1 1 - 1 1 1 1 1 Returns the results of these calculations
Calculations on vectors x lt- 120 sum(x) mean(x) sd(x) sqrt(x) rank(x)sort(x) Calculates for the vector x its sum mean standard deviation and square root etc x lt- 120 y lt- 120 x + y Calculates the sum for each element in the vectors x and y
Data Types
Numeric data 1 2 3 x lt- c(1 2 3) x isnumeric(x) ascharacter(x) Creates a numeric vector checks for the data type and converts it into a character vectorCharacter data a b c x lt- c(1 2 3) x ischaracter(x) asnumeric(x)
Creates a character vector checks for the data type and converts it into a numeric vectorLogical data TRUE FALSE TRUE 110 lt 5 Returns TRUE where x is lt 5
Object Types
vectors ordered collection of numeric character complex and logical values factors special type vectors with grouping information of its components data frames two dimensional structures with different data types matrices two dimensional structures with data of same type arrays multidimensional arrays of vectors lists general form of vectors with different types of elements functions piece of code
Subsetting Syntax
my_object[index] Subsettingof one dimensional objects like vectors and factors Returns elements with positions in indexmy_object[rowindex colindex] Subsettingof two dimensional objects like matrices and data framesmy_object[rowindex colindex dim] Subsettingof three dimensional objects like arraysdim(my_object) Returns the numbers of row and columnmy_logicallt-(my_objectgt 10) Generates a logical vector as examplemy_object[my_logical] Returns the elements where my_logical contains TRUE valuesmy_object$Name1 Returns the lsquoName1 column in the my_objectdata frame
Vector amp List
bull vector an ordered collection of data of the same type gt a = c(751)gt a[2][1] 5
bull list an ordered collection of data of arbitrary types gt doe = list(name=johnage=28married=F)gt doe$name[1] johnldquogt doe$age[1] 28bull Typically vector elements are accessed by their index (an integer)
list elements by their name (a character string) But both types support both access methods
Programming in R
Ifelse StatementExample x lt- 110 ifelse(xlt5 | xgt8 x 0)
For Loop Example mean mydf lt- irismyve lt- NULLfor(i in 1length(mydf[1])) myve lt- c(myve mean(asvector(asmatrix(mydf[i1
3]))))
While Loop Example z lt- 0while(zlt5) z lt- z+2print(z)
Writing your own functionsgt f=function(x)x^2+2xgtf(3)[1] 15
Graphics
library(UsingR)scatterwithhist(faithful$eruptionsfaithf ul$waiting)
Reference
2007 年 R 統計軟體研習會 ------- 蔡政安 教授httpwwwstateduntuedutw2007conferenceindexhtm
An Introduction to R
httpcranr-projectorgdocmanualsR-intropdf
~The End~
Thanks for your listening
Basic Usage of R
class(object)displays the object type
str(object)displays the internal type and structure of an R objectgtstr(m)num [14 13] 0248 0589 -0589 0504 1524
attributes(object)Returns an objects attribute listgt attributes(m)$dim[1] 4 3
dir() Reads content of current working directory
getwd() Returns current working directory
setwd(homeuser) Changes current working directory to user specified directory
Data Import
readdelim(clipboard header=T) Command to copyamppastetables from Excel or other programs into R If the header argument isset to FALSE then the first line of the data set will not be used as column titlesscan(my_file) reads vectorarray into vector from file or keyboardmy_framelt-readdelim(ldquocAffymetrixaffy1txt nastrings= fill=TRUE header=T sep=t) The function readdelim() is often more flexible for importing tables with empty fields and long character strings (eg gene descriptions)
It supports data import on the webDifferent coding of missing values (nastrings=ldquoNArdquoor ldquordquo)
Data columns can be separated by TAB comma or semicolon (sep=ldquordquo)
Data Import
There are some alternatives for reading data as followingsmy_framelt-readtable(file=ldquopath header=TRUE sep=t)Reads in table with info on column headers and field separators datalt-readtable(httpwwwcmuedutwexampletxt header=TRUE)
my_framelt-readcsv(file=ldquopathldquo header=TRUE) reads csvfile with comma separated value You can skip lines read a limited number of lines different decimal separator and more importing options The foreign package can read files from Stata SAS and SPSS
Data Export
writetable(iris clipboard sep=t colnames=NA quote=F) Command to copyamppastefrom R into Excel or other programs It writes the data of an R data frame object into the clipbroardfrom where it can be pasted into other applicationswritetable(dataframe file=ldquofile path sep=t colnames= NA) Writes data frame to a tab-delimited text file The argument colnames= NA makes sure that the titles align with columns when rowindex names are exported (default)write(x file=file path) Writes matrix data to a file
Basic Operators
Comparison operators equal == not equal = greaterless than gt lt greaterless than or equal gt= lt= Example 1 == 1 Returns TRUE
1048710Logical operators ANDamp
x lt- 110 y lt- 101 Creates the sample vectors x and y x gt y amp x gt 5 Returns TRUE where both comparisons return TRUE OR |
x == y | x = y Returns TRUE where at least one comparison returns TRUE NOT
x gt y The sign returns the negation (opposite) of a logical vector
Basic Operators
Calculations Four basic arithmetic functions addition subtraction multiplication and division 1 + 1 1 - 1 1 1 1 1 Returns the results of these calculations
Calculations on vectors x lt- 120 sum(x) mean(x) sd(x) sqrt(x) rank(x)sort(x) Calculates for the vector x its sum mean standard deviation and square root etc x lt- 120 y lt- 120 x + y Calculates the sum for each element in the vectors x and y
Data Types
Numeric data 1 2 3 x lt- c(1 2 3) x isnumeric(x) ascharacter(x) Creates a numeric vector checks for the data type and converts it into a character vectorCharacter data a b c x lt- c(1 2 3) x ischaracter(x) asnumeric(x)
Creates a character vector checks for the data type and converts it into a numeric vectorLogical data TRUE FALSE TRUE 110 lt 5 Returns TRUE where x is lt 5
Object Types
vectors ordered collection of numeric character complex and logical values factors special type vectors with grouping information of its components data frames two dimensional structures with different data types matrices two dimensional structures with data of same type arrays multidimensional arrays of vectors lists general form of vectors with different types of elements functions piece of code
Subsetting Syntax
my_object[index] Subsettingof one dimensional objects like vectors and factors Returns elements with positions in indexmy_object[rowindex colindex] Subsettingof two dimensional objects like matrices and data framesmy_object[rowindex colindex dim] Subsettingof three dimensional objects like arraysdim(my_object) Returns the numbers of row and columnmy_logicallt-(my_objectgt 10) Generates a logical vector as examplemy_object[my_logical] Returns the elements where my_logical contains TRUE valuesmy_object$Name1 Returns the lsquoName1 column in the my_objectdata frame
Vector amp List
bull vector an ordered collection of data of the same type gt a = c(751)gt a[2][1] 5
bull list an ordered collection of data of arbitrary types gt doe = list(name=johnage=28married=F)gt doe$name[1] johnldquogt doe$age[1] 28bull Typically vector elements are accessed by their index (an integer)
list elements by their name (a character string) But both types support both access methods
Programming in R
Ifelse StatementExample x lt- 110 ifelse(xlt5 | xgt8 x 0)
For Loop Example mean mydf lt- irismyve lt- NULLfor(i in 1length(mydf[1])) myve lt- c(myve mean(asvector(asmatrix(mydf[i1
3]))))
While Loop Example z lt- 0while(zlt5) z lt- z+2print(z)
Writing your own functionsgt f=function(x)x^2+2xgtf(3)[1] 15
Graphics
library(UsingR)scatterwithhist(faithful$eruptionsfaithf ul$waiting)
Reference
2007 年 R 統計軟體研習會 ------- 蔡政安 教授httpwwwstateduntuedutw2007conferenceindexhtm
An Introduction to R
httpcranr-projectorgdocmanualsR-intropdf
~The End~
Thanks for your listening
Data Import
readdelim(clipboard header=T) Command to copyamppastetables from Excel or other programs into R If the header argument isset to FALSE then the first line of the data set will not be used as column titlesscan(my_file) reads vectorarray into vector from file or keyboardmy_framelt-readdelim(ldquocAffymetrixaffy1txt nastrings= fill=TRUE header=T sep=t) The function readdelim() is often more flexible for importing tables with empty fields and long character strings (eg gene descriptions)
It supports data import on the webDifferent coding of missing values (nastrings=ldquoNArdquoor ldquordquo)
Data columns can be separated by TAB comma or semicolon (sep=ldquordquo)
Data Import
There are some alternatives for reading data as followingsmy_framelt-readtable(file=ldquopath header=TRUE sep=t)Reads in table with info on column headers and field separators datalt-readtable(httpwwwcmuedutwexampletxt header=TRUE)
my_framelt-readcsv(file=ldquopathldquo header=TRUE) reads csvfile with comma separated value You can skip lines read a limited number of lines different decimal separator and more importing options The foreign package can read files from Stata SAS and SPSS
Data Export
writetable(iris clipboard sep=t colnames=NA quote=F) Command to copyamppastefrom R into Excel or other programs It writes the data of an R data frame object into the clipbroardfrom where it can be pasted into other applicationswritetable(dataframe file=ldquofile path sep=t colnames= NA) Writes data frame to a tab-delimited text file The argument colnames= NA makes sure that the titles align with columns when rowindex names are exported (default)write(x file=file path) Writes matrix data to a file
Basic Operators
Comparison operators equal == not equal = greaterless than gt lt greaterless than or equal gt= lt= Example 1 == 1 Returns TRUE
1048710Logical operators ANDamp
x lt- 110 y lt- 101 Creates the sample vectors x and y x gt y amp x gt 5 Returns TRUE where both comparisons return TRUE OR |
x == y | x = y Returns TRUE where at least one comparison returns TRUE NOT
x gt y The sign returns the negation (opposite) of a logical vector
Basic Operators
Calculations Four basic arithmetic functions addition subtraction multiplication and division 1 + 1 1 - 1 1 1 1 1 Returns the results of these calculations
Calculations on vectors x lt- 120 sum(x) mean(x) sd(x) sqrt(x) rank(x)sort(x) Calculates for the vector x its sum mean standard deviation and square root etc x lt- 120 y lt- 120 x + y Calculates the sum for each element in the vectors x and y
Data Types
Numeric data 1 2 3 x lt- c(1 2 3) x isnumeric(x) ascharacter(x) Creates a numeric vector checks for the data type and converts it into a character vectorCharacter data a b c x lt- c(1 2 3) x ischaracter(x) asnumeric(x)
Creates a character vector checks for the data type and converts it into a numeric vectorLogical data TRUE FALSE TRUE 110 lt 5 Returns TRUE where x is lt 5
Object Types
vectors ordered collection of numeric character complex and logical values factors special type vectors with grouping information of its components data frames two dimensional structures with different data types matrices two dimensional structures with data of same type arrays multidimensional arrays of vectors lists general form of vectors with different types of elements functions piece of code
Subsetting Syntax
my_object[index] Subsettingof one dimensional objects like vectors and factors Returns elements with positions in indexmy_object[rowindex colindex] Subsettingof two dimensional objects like matrices and data framesmy_object[rowindex colindex dim] Subsettingof three dimensional objects like arraysdim(my_object) Returns the numbers of row and columnmy_logicallt-(my_objectgt 10) Generates a logical vector as examplemy_object[my_logical] Returns the elements where my_logical contains TRUE valuesmy_object$Name1 Returns the lsquoName1 column in the my_objectdata frame
Vector amp List
bull vector an ordered collection of data of the same type gt a = c(751)gt a[2][1] 5
bull list an ordered collection of data of arbitrary types gt doe = list(name=johnage=28married=F)gt doe$name[1] johnldquogt doe$age[1] 28bull Typically vector elements are accessed by their index (an integer)
list elements by their name (a character string) But both types support both access methods
Programming in R
Ifelse StatementExample x lt- 110 ifelse(xlt5 | xgt8 x 0)
For Loop Example mean mydf lt- irismyve lt- NULLfor(i in 1length(mydf[1])) myve lt- c(myve mean(asvector(asmatrix(mydf[i1
3]))))
While Loop Example z lt- 0while(zlt5) z lt- z+2print(z)
Writing your own functionsgt f=function(x)x^2+2xgtf(3)[1] 15
Graphics
library(UsingR)scatterwithhist(faithful$eruptionsfaithf ul$waiting)
Reference
2007 年 R 統計軟體研習會 ------- 蔡政安 教授httpwwwstateduntuedutw2007conferenceindexhtm
An Introduction to R
httpcranr-projectorgdocmanualsR-intropdf
~The End~
Thanks for your listening
Data Import
There are some alternatives for reading data as followingsmy_framelt-readtable(file=ldquopath header=TRUE sep=t)Reads in table with info on column headers and field separators datalt-readtable(httpwwwcmuedutwexampletxt header=TRUE)
my_framelt-readcsv(file=ldquopathldquo header=TRUE) reads csvfile with comma separated value You can skip lines read a limited number of lines different decimal separator and more importing options The foreign package can read files from Stata SAS and SPSS
Data Export
writetable(iris clipboard sep=t colnames=NA quote=F) Command to copyamppastefrom R into Excel or other programs It writes the data of an R data frame object into the clipbroardfrom where it can be pasted into other applicationswritetable(dataframe file=ldquofile path sep=t colnames= NA) Writes data frame to a tab-delimited text file The argument colnames= NA makes sure that the titles align with columns when rowindex names are exported (default)write(x file=file path) Writes matrix data to a file
Basic Operators
Comparison operators equal == not equal = greaterless than gt lt greaterless than or equal gt= lt= Example 1 == 1 Returns TRUE
1048710Logical operators ANDamp
x lt- 110 y lt- 101 Creates the sample vectors x and y x gt y amp x gt 5 Returns TRUE where both comparisons return TRUE OR |
x == y | x = y Returns TRUE where at least one comparison returns TRUE NOT
x gt y The sign returns the negation (opposite) of a logical vector
Basic Operators
Calculations Four basic arithmetic functions addition subtraction multiplication and division 1 + 1 1 - 1 1 1 1 1 Returns the results of these calculations
Calculations on vectors x lt- 120 sum(x) mean(x) sd(x) sqrt(x) rank(x)sort(x) Calculates for the vector x its sum mean standard deviation and square root etc x lt- 120 y lt- 120 x + y Calculates the sum for each element in the vectors x and y
Data Types
Numeric data 1 2 3 x lt- c(1 2 3) x isnumeric(x) ascharacter(x) Creates a numeric vector checks for the data type and converts it into a character vectorCharacter data a b c x lt- c(1 2 3) x ischaracter(x) asnumeric(x)
Creates a character vector checks for the data type and converts it into a numeric vectorLogical data TRUE FALSE TRUE 110 lt 5 Returns TRUE where x is lt 5
Object Types
vectors ordered collection of numeric character complex and logical values factors special type vectors with grouping information of its components data frames two dimensional structures with different data types matrices two dimensional structures with data of same type arrays multidimensional arrays of vectors lists general form of vectors with different types of elements functions piece of code
Subsetting Syntax
my_object[index] Subsettingof one dimensional objects like vectors and factors Returns elements with positions in indexmy_object[rowindex colindex] Subsettingof two dimensional objects like matrices and data framesmy_object[rowindex colindex dim] Subsettingof three dimensional objects like arraysdim(my_object) Returns the numbers of row and columnmy_logicallt-(my_objectgt 10) Generates a logical vector as examplemy_object[my_logical] Returns the elements where my_logical contains TRUE valuesmy_object$Name1 Returns the lsquoName1 column in the my_objectdata frame
Vector amp List
bull vector an ordered collection of data of the same type gt a = c(751)gt a[2][1] 5
bull list an ordered collection of data of arbitrary types gt doe = list(name=johnage=28married=F)gt doe$name[1] johnldquogt doe$age[1] 28bull Typically vector elements are accessed by their index (an integer)
list elements by their name (a character string) But both types support both access methods
Programming in R
Ifelse StatementExample x lt- 110 ifelse(xlt5 | xgt8 x 0)
For Loop Example mean mydf lt- irismyve lt- NULLfor(i in 1length(mydf[1])) myve lt- c(myve mean(asvector(asmatrix(mydf[i1
3]))))
While Loop Example z lt- 0while(zlt5) z lt- z+2print(z)
Writing your own functionsgt f=function(x)x^2+2xgtf(3)[1] 15
Graphics
library(UsingR)scatterwithhist(faithful$eruptionsfaithf ul$waiting)
Reference
2007 年 R 統計軟體研習會 ------- 蔡政安 教授httpwwwstateduntuedutw2007conferenceindexhtm
An Introduction to R
httpcranr-projectorgdocmanualsR-intropdf
~The End~
Thanks for your listening
Data Export
writetable(iris clipboard sep=t colnames=NA quote=F) Command to copyamppastefrom R into Excel or other programs It writes the data of an R data frame object into the clipbroardfrom where it can be pasted into other applicationswritetable(dataframe file=ldquofile path sep=t colnames= NA) Writes data frame to a tab-delimited text file The argument colnames= NA makes sure that the titles align with columns when rowindex names are exported (default)write(x file=file path) Writes matrix data to a file
Basic Operators
Comparison operators equal == not equal = greaterless than gt lt greaterless than or equal gt= lt= Example 1 == 1 Returns TRUE
1048710Logical operators ANDamp
x lt- 110 y lt- 101 Creates the sample vectors x and y x gt y amp x gt 5 Returns TRUE where both comparisons return TRUE OR |
x == y | x = y Returns TRUE where at least one comparison returns TRUE NOT
x gt y The sign returns the negation (opposite) of a logical vector
Basic Operators
Calculations Four basic arithmetic functions addition subtraction multiplication and division 1 + 1 1 - 1 1 1 1 1 Returns the results of these calculations
Calculations on vectors x lt- 120 sum(x) mean(x) sd(x) sqrt(x) rank(x)sort(x) Calculates for the vector x its sum mean standard deviation and square root etc x lt- 120 y lt- 120 x + y Calculates the sum for each element in the vectors x and y
Data Types
Numeric data 1 2 3 x lt- c(1 2 3) x isnumeric(x) ascharacter(x) Creates a numeric vector checks for the data type and converts it into a character vectorCharacter data a b c x lt- c(1 2 3) x ischaracter(x) asnumeric(x)
Creates a character vector checks for the data type and converts it into a numeric vectorLogical data TRUE FALSE TRUE 110 lt 5 Returns TRUE where x is lt 5
Object Types
vectors ordered collection of numeric character complex and logical values factors special type vectors with grouping information of its components data frames two dimensional structures with different data types matrices two dimensional structures with data of same type arrays multidimensional arrays of vectors lists general form of vectors with different types of elements functions piece of code
Subsetting Syntax
my_object[index] Subsettingof one dimensional objects like vectors and factors Returns elements with positions in indexmy_object[rowindex colindex] Subsettingof two dimensional objects like matrices and data framesmy_object[rowindex colindex dim] Subsettingof three dimensional objects like arraysdim(my_object) Returns the numbers of row and columnmy_logicallt-(my_objectgt 10) Generates a logical vector as examplemy_object[my_logical] Returns the elements where my_logical contains TRUE valuesmy_object$Name1 Returns the lsquoName1 column in the my_objectdata frame
Vector amp List
bull vector an ordered collection of data of the same type gt a = c(751)gt a[2][1] 5
bull list an ordered collection of data of arbitrary types gt doe = list(name=johnage=28married=F)gt doe$name[1] johnldquogt doe$age[1] 28bull Typically vector elements are accessed by their index (an integer)
list elements by their name (a character string) But both types support both access methods
Programming in R
Ifelse StatementExample x lt- 110 ifelse(xlt5 | xgt8 x 0)
For Loop Example mean mydf lt- irismyve lt- NULLfor(i in 1length(mydf[1])) myve lt- c(myve mean(asvector(asmatrix(mydf[i1
3]))))
While Loop Example z lt- 0while(zlt5) z lt- z+2print(z)
Writing your own functionsgt f=function(x)x^2+2xgtf(3)[1] 15
Graphics
library(UsingR)scatterwithhist(faithful$eruptionsfaithf ul$waiting)
Reference
2007 年 R 統計軟體研習會 ------- 蔡政安 教授httpwwwstateduntuedutw2007conferenceindexhtm
An Introduction to R
httpcranr-projectorgdocmanualsR-intropdf
~The End~
Thanks for your listening
Basic Operators
Comparison operators equal == not equal = greaterless than gt lt greaterless than or equal gt= lt= Example 1 == 1 Returns TRUE
1048710Logical operators ANDamp
x lt- 110 y lt- 101 Creates the sample vectors x and y x gt y amp x gt 5 Returns TRUE where both comparisons return TRUE OR |
x == y | x = y Returns TRUE where at least one comparison returns TRUE NOT
x gt y The sign returns the negation (opposite) of a logical vector
Basic Operators
Calculations Four basic arithmetic functions addition subtraction multiplication and division 1 + 1 1 - 1 1 1 1 1 Returns the results of these calculations
Calculations on vectors x lt- 120 sum(x) mean(x) sd(x) sqrt(x) rank(x)sort(x) Calculates for the vector x its sum mean standard deviation and square root etc x lt- 120 y lt- 120 x + y Calculates the sum for each element in the vectors x and y
Data Types
Numeric data 1 2 3 x lt- c(1 2 3) x isnumeric(x) ascharacter(x) Creates a numeric vector checks for the data type and converts it into a character vectorCharacter data a b c x lt- c(1 2 3) x ischaracter(x) asnumeric(x)
Creates a character vector checks for the data type and converts it into a numeric vectorLogical data TRUE FALSE TRUE 110 lt 5 Returns TRUE where x is lt 5
Object Types
vectors ordered collection of numeric character complex and logical values factors special type vectors with grouping information of its components data frames two dimensional structures with different data types matrices two dimensional structures with data of same type arrays multidimensional arrays of vectors lists general form of vectors with different types of elements functions piece of code
Subsetting Syntax
my_object[index] Subsettingof one dimensional objects like vectors and factors Returns elements with positions in indexmy_object[rowindex colindex] Subsettingof two dimensional objects like matrices and data framesmy_object[rowindex colindex dim] Subsettingof three dimensional objects like arraysdim(my_object) Returns the numbers of row and columnmy_logicallt-(my_objectgt 10) Generates a logical vector as examplemy_object[my_logical] Returns the elements where my_logical contains TRUE valuesmy_object$Name1 Returns the lsquoName1 column in the my_objectdata frame
Vector amp List
bull vector an ordered collection of data of the same type gt a = c(751)gt a[2][1] 5
bull list an ordered collection of data of arbitrary types gt doe = list(name=johnage=28married=F)gt doe$name[1] johnldquogt doe$age[1] 28bull Typically vector elements are accessed by their index (an integer)
list elements by their name (a character string) But both types support both access methods
Programming in R
Ifelse StatementExample x lt- 110 ifelse(xlt5 | xgt8 x 0)
For Loop Example mean mydf lt- irismyve lt- NULLfor(i in 1length(mydf[1])) myve lt- c(myve mean(asvector(asmatrix(mydf[i1
3]))))
While Loop Example z lt- 0while(zlt5) z lt- z+2print(z)
Writing your own functionsgt f=function(x)x^2+2xgtf(3)[1] 15
Graphics
library(UsingR)scatterwithhist(faithful$eruptionsfaithf ul$waiting)
Reference
2007 年 R 統計軟體研習會 ------- 蔡政安 教授httpwwwstateduntuedutw2007conferenceindexhtm
An Introduction to R
httpcranr-projectorgdocmanualsR-intropdf
~The End~
Thanks for your listening
Basic Operators
Calculations Four basic arithmetic functions addition subtraction multiplication and division 1 + 1 1 - 1 1 1 1 1 Returns the results of these calculations
Calculations on vectors x lt- 120 sum(x) mean(x) sd(x) sqrt(x) rank(x)sort(x) Calculates for the vector x its sum mean standard deviation and square root etc x lt- 120 y lt- 120 x + y Calculates the sum for each element in the vectors x and y
Data Types
Numeric data 1 2 3 x lt- c(1 2 3) x isnumeric(x) ascharacter(x) Creates a numeric vector checks for the data type and converts it into a character vectorCharacter data a b c x lt- c(1 2 3) x ischaracter(x) asnumeric(x)
Creates a character vector checks for the data type and converts it into a numeric vectorLogical data TRUE FALSE TRUE 110 lt 5 Returns TRUE where x is lt 5
Object Types
vectors ordered collection of numeric character complex and logical values factors special type vectors with grouping information of its components data frames two dimensional structures with different data types matrices two dimensional structures with data of same type arrays multidimensional arrays of vectors lists general form of vectors with different types of elements functions piece of code
Subsetting Syntax
my_object[index] Subsettingof one dimensional objects like vectors and factors Returns elements with positions in indexmy_object[rowindex colindex] Subsettingof two dimensional objects like matrices and data framesmy_object[rowindex colindex dim] Subsettingof three dimensional objects like arraysdim(my_object) Returns the numbers of row and columnmy_logicallt-(my_objectgt 10) Generates a logical vector as examplemy_object[my_logical] Returns the elements where my_logical contains TRUE valuesmy_object$Name1 Returns the lsquoName1 column in the my_objectdata frame
Vector amp List
bull vector an ordered collection of data of the same type gt a = c(751)gt a[2][1] 5
bull list an ordered collection of data of arbitrary types gt doe = list(name=johnage=28married=F)gt doe$name[1] johnldquogt doe$age[1] 28bull Typically vector elements are accessed by their index (an integer)
list elements by their name (a character string) But both types support both access methods
Programming in R
Ifelse StatementExample x lt- 110 ifelse(xlt5 | xgt8 x 0)
For Loop Example mean mydf lt- irismyve lt- NULLfor(i in 1length(mydf[1])) myve lt- c(myve mean(asvector(asmatrix(mydf[i1
3]))))
While Loop Example z lt- 0while(zlt5) z lt- z+2print(z)
Writing your own functionsgt f=function(x)x^2+2xgtf(3)[1] 15
Graphics
library(UsingR)scatterwithhist(faithful$eruptionsfaithf ul$waiting)
Reference
2007 年 R 統計軟體研習會 ------- 蔡政安 教授httpwwwstateduntuedutw2007conferenceindexhtm
An Introduction to R
httpcranr-projectorgdocmanualsR-intropdf
~The End~
Thanks for your listening
Data Types
Numeric data 1 2 3 x lt- c(1 2 3) x isnumeric(x) ascharacter(x) Creates a numeric vector checks for the data type and converts it into a character vectorCharacter data a b c x lt- c(1 2 3) x ischaracter(x) asnumeric(x)
Creates a character vector checks for the data type and converts it into a numeric vectorLogical data TRUE FALSE TRUE 110 lt 5 Returns TRUE where x is lt 5
Object Types
vectors ordered collection of numeric character complex and logical values factors special type vectors with grouping information of its components data frames two dimensional structures with different data types matrices two dimensional structures with data of same type arrays multidimensional arrays of vectors lists general form of vectors with different types of elements functions piece of code
Subsetting Syntax
my_object[index] Subsettingof one dimensional objects like vectors and factors Returns elements with positions in indexmy_object[rowindex colindex] Subsettingof two dimensional objects like matrices and data framesmy_object[rowindex colindex dim] Subsettingof three dimensional objects like arraysdim(my_object) Returns the numbers of row and columnmy_logicallt-(my_objectgt 10) Generates a logical vector as examplemy_object[my_logical] Returns the elements where my_logical contains TRUE valuesmy_object$Name1 Returns the lsquoName1 column in the my_objectdata frame
Vector amp List
bull vector an ordered collection of data of the same type gt a = c(751)gt a[2][1] 5
bull list an ordered collection of data of arbitrary types gt doe = list(name=johnage=28married=F)gt doe$name[1] johnldquogt doe$age[1] 28bull Typically vector elements are accessed by their index (an integer)
list elements by their name (a character string) But both types support both access methods
Programming in R
Ifelse StatementExample x lt- 110 ifelse(xlt5 | xgt8 x 0)
For Loop Example mean mydf lt- irismyve lt- NULLfor(i in 1length(mydf[1])) myve lt- c(myve mean(asvector(asmatrix(mydf[i1
3]))))
While Loop Example z lt- 0while(zlt5) z lt- z+2print(z)
Writing your own functionsgt f=function(x)x^2+2xgtf(3)[1] 15
Graphics
library(UsingR)scatterwithhist(faithful$eruptionsfaithf ul$waiting)
Reference
2007 年 R 統計軟體研習會 ------- 蔡政安 教授httpwwwstateduntuedutw2007conferenceindexhtm
An Introduction to R
httpcranr-projectorgdocmanualsR-intropdf
~The End~
Thanks for your listening
Object Types
vectors ordered collection of numeric character complex and logical values factors special type vectors with grouping information of its components data frames two dimensional structures with different data types matrices two dimensional structures with data of same type arrays multidimensional arrays of vectors lists general form of vectors with different types of elements functions piece of code
Subsetting Syntax
my_object[index] Subsettingof one dimensional objects like vectors and factors Returns elements with positions in indexmy_object[rowindex colindex] Subsettingof two dimensional objects like matrices and data framesmy_object[rowindex colindex dim] Subsettingof three dimensional objects like arraysdim(my_object) Returns the numbers of row and columnmy_logicallt-(my_objectgt 10) Generates a logical vector as examplemy_object[my_logical] Returns the elements where my_logical contains TRUE valuesmy_object$Name1 Returns the lsquoName1 column in the my_objectdata frame
Vector amp List
bull vector an ordered collection of data of the same type gt a = c(751)gt a[2][1] 5
bull list an ordered collection of data of arbitrary types gt doe = list(name=johnage=28married=F)gt doe$name[1] johnldquogt doe$age[1] 28bull Typically vector elements are accessed by their index (an integer)
list elements by their name (a character string) But both types support both access methods
Programming in R
Ifelse StatementExample x lt- 110 ifelse(xlt5 | xgt8 x 0)
For Loop Example mean mydf lt- irismyve lt- NULLfor(i in 1length(mydf[1])) myve lt- c(myve mean(asvector(asmatrix(mydf[i1
3]))))
While Loop Example z lt- 0while(zlt5) z lt- z+2print(z)
Writing your own functionsgt f=function(x)x^2+2xgtf(3)[1] 15
Graphics
library(UsingR)scatterwithhist(faithful$eruptionsfaithf ul$waiting)
Reference
2007 年 R 統計軟體研習會 ------- 蔡政安 教授httpwwwstateduntuedutw2007conferenceindexhtm
An Introduction to R
httpcranr-projectorgdocmanualsR-intropdf
~The End~
Thanks for your listening
Subsetting Syntax
my_object[index] Subsettingof one dimensional objects like vectors and factors Returns elements with positions in indexmy_object[rowindex colindex] Subsettingof two dimensional objects like matrices and data framesmy_object[rowindex colindex dim] Subsettingof three dimensional objects like arraysdim(my_object) Returns the numbers of row and columnmy_logicallt-(my_objectgt 10) Generates a logical vector as examplemy_object[my_logical] Returns the elements where my_logical contains TRUE valuesmy_object$Name1 Returns the lsquoName1 column in the my_objectdata frame
Vector amp List
bull vector an ordered collection of data of the same type gt a = c(751)gt a[2][1] 5
bull list an ordered collection of data of arbitrary types gt doe = list(name=johnage=28married=F)gt doe$name[1] johnldquogt doe$age[1] 28bull Typically vector elements are accessed by their index (an integer)
list elements by their name (a character string) But both types support both access methods
Programming in R
Ifelse StatementExample x lt- 110 ifelse(xlt5 | xgt8 x 0)
For Loop Example mean mydf lt- irismyve lt- NULLfor(i in 1length(mydf[1])) myve lt- c(myve mean(asvector(asmatrix(mydf[i1
3]))))
While Loop Example z lt- 0while(zlt5) z lt- z+2print(z)
Writing your own functionsgt f=function(x)x^2+2xgtf(3)[1] 15
Graphics
library(UsingR)scatterwithhist(faithful$eruptionsfaithf ul$waiting)
Reference
2007 年 R 統計軟體研習會 ------- 蔡政安 教授httpwwwstateduntuedutw2007conferenceindexhtm
An Introduction to R
httpcranr-projectorgdocmanualsR-intropdf
~The End~
Thanks for your listening
Vector amp List
bull vector an ordered collection of data of the same type gt a = c(751)gt a[2][1] 5
bull list an ordered collection of data of arbitrary types gt doe = list(name=johnage=28married=F)gt doe$name[1] johnldquogt doe$age[1] 28bull Typically vector elements are accessed by their index (an integer)
list elements by their name (a character string) But both types support both access methods
Programming in R
Ifelse StatementExample x lt- 110 ifelse(xlt5 | xgt8 x 0)
For Loop Example mean mydf lt- irismyve lt- NULLfor(i in 1length(mydf[1])) myve lt- c(myve mean(asvector(asmatrix(mydf[i1
3]))))
While Loop Example z lt- 0while(zlt5) z lt- z+2print(z)
Writing your own functionsgt f=function(x)x^2+2xgtf(3)[1] 15
Graphics
library(UsingR)scatterwithhist(faithful$eruptionsfaithf ul$waiting)
Reference
2007 年 R 統計軟體研習會 ------- 蔡政安 教授httpwwwstateduntuedutw2007conferenceindexhtm
An Introduction to R
httpcranr-projectorgdocmanualsR-intropdf
~The End~
Thanks for your listening
Programming in R
Ifelse StatementExample x lt- 110 ifelse(xlt5 | xgt8 x 0)
For Loop Example mean mydf lt- irismyve lt- NULLfor(i in 1length(mydf[1])) myve lt- c(myve mean(asvector(asmatrix(mydf[i1
3]))))
While Loop Example z lt- 0while(zlt5) z lt- z+2print(z)
Writing your own functionsgt f=function(x)x^2+2xgtf(3)[1] 15
Graphics
library(UsingR)scatterwithhist(faithful$eruptionsfaithf ul$waiting)
Reference
2007 年 R 統計軟體研習會 ------- 蔡政安 教授httpwwwstateduntuedutw2007conferenceindexhtm
An Introduction to R
httpcranr-projectorgdocmanualsR-intropdf
~The End~
Thanks for your listening
While Loop Example z lt- 0while(zlt5) z lt- z+2print(z)
Writing your own functionsgt f=function(x)x^2+2xgtf(3)[1] 15
Graphics
library(UsingR)scatterwithhist(faithful$eruptionsfaithf ul$waiting)
Reference
2007 年 R 統計軟體研習會 ------- 蔡政安 教授httpwwwstateduntuedutw2007conferenceindexhtm
An Introduction to R
httpcranr-projectorgdocmanualsR-intropdf
~The End~
Thanks for your listening
Graphics
library(UsingR)scatterwithhist(faithful$eruptionsfaithf ul$waiting)
Reference
2007 年 R 統計軟體研習會 ------- 蔡政安 教授httpwwwstateduntuedutw2007conferenceindexhtm
An Introduction to R
httpcranr-projectorgdocmanualsR-intropdf
~The End~
Thanks for your listening
Reference
2007 年 R 統計軟體研習會 ------- 蔡政安 教授httpwwwstateduntuedutw2007conferenceindexhtm
An Introduction to R
httpcranr-projectorgdocmanualsR-intropdf
~The End~
Thanks for your listening
~The End~
Thanks for your listening