You are on page 1of 8

R

• Statistical analysis system created


Presentation Title Goes Here by Ross Ihaka and Robert Gentleman
Introduction to R
…presentation subtitle. in 1996
• An implementation of the S language
Data Manipulation • Downloadable free at http://cran.r-
and Statistical analysis project.org/

Some References

• Venables W.N. & Smith D.M., ‘An


Introduction to R’, downloadable at
http://cran.r-
project.org/doc/manuals/R-intro.pdf
• R Development Core Team, ‘R-
Language Definition’, downloadable
at http://cran.r-
project.org/manuals/R-lang.pdf
Installing Packages
Basic concepts
• R is an object-oriented programming
language
• Everything in R
- data, results from analyses, functions –
is an object

Basic concepts Basic concepts


• > is the R prompt • Objects can be assigned a value by using
assignment operator “<-” or “=”
• If a command is not complete at the end of a
line, there will be a “+” instead of the R prompt (Note: sometimes the “=” does not work)
to complete the command
• If there is a need to use backslash “\”, use
• Commands are separated by “;” OR a new line double backslash instead “\\”

• Commands can be grouped together using curly • Comments start with a hash “#”
brackets - “{” and “}”
Basic concepts Basic concepts
• Objects are accessed by names
• R has reserved words which may not be
Object names can be composed of letters, used as object names
digits, or period “.”
one-letter reserved words: c, q, s, t, C, D, F, I,
It should not start with a number and T
Avoid spaces in object names FALSE, TRUE, Inf, NA, NaN, NULL, break, else, for,
function, if, in, next, repeat, and while
• R is case-sensitive
variety is different from Variety

Naming objects in R Data types


• Names of system objects
• Character – values are text strings
like mean, median, sqrt, pi, var – enclosed in double quotes “ ”
are not reserved and can be used as object
e.g. > text <- “a”
names
> text2 <- “IR64”
but is not a good idea as it can lead to • Numeric – values are real numbers
unnecessary confusion. Avoid doing so. e.g. > num <- -0.5
> num <- 3+1
• Logical – create logical values of either
TRUE or FALSE
Basic operators Calculations
Arithmetic operators
Comparison operators • addition: +
• equal: == • subtraction : -
• not equal: != • multiplication: *
• greater/less than: > < • division: /
• greater/less than or equal: >= <= Some basic functions
Logical operators • sum()
• mean()
• AND: &
• sd()
• OR : |
• sqrt()
• NOT: !
• log()

Vector
• collection of numeric, character or logical
values (one-dimensional array)

• Functions and logical operators work


on each element of the vector
Creating Vectors Manipulating Vectors

Functions of Vectors Matrices


• two dimensional structures with data of same
type and consists of rows and columns (two-
dimensional array)
Useful commands for matrices Data Frames
• or data set is a special type of list in which
each variable should have the same
number of observations
• similar to matrix, but can store different
data types
e.g. > class
gender weight
> 1 F 115
> 2 M 117
> 3 F 120
> 4 M 123
> 5 M 126

Some useful commands

• Online help
> help.start()

• Get help for a function


> help(aov) or ?aov
Some useful commands

• Diverting output as a textfile


> sink(“filename.text")
> R command(s)
> sink()
• Quit R
> q()

R Script
• text file containing a list of R commands
and usually given the extension .R
• can be saved for later use
• for documentation and traceability
• To create a script
Click File/New Script
• To open a script
Click File/Open Script
Thank you!

You might also like