1.R Unit 1
1.R Unit 1
quality graphics
– user written functions & sets of functions
An arrow (<-) formed by a smaller than character
and a hyphen without a space!
setwd("c:/docs/mydir")
R Help
Once R is installed, there is a comprehensive
built-in help system. At the program's
command prompt you can use any of the
following:
help.start() # general help
help(foo) # help about function foo
?foo # same thing
apropos("foo") # list all function containing string foo
example(foo) # show an example of function foo
R Datasets
R comes with a number of sample datasets
that you can experiment with. Type
> data( )
to see the available datasets. The results
will depend on which packages you have
loaded. Type
help(datasetname)
for details on a sample dataset.
R Packages
One of the strengths of R is that the system can easily be
extended.
> search()
[1] ".GlobalEnv" "package:stats" "package:graphics"
[4] "package:grDevices" "package:datasets" "package:utils"
[7] "package:methods" "Autoloads" "package:base"
R Packages
The function library used to list all the available libraries on
your system with a short description.
Run the function without any arguments
> library()
Packages in library 'C:/PROGRA~1/R/R-25~1.0/library':
base The R Base Package
Boot Bootstrap R (S-Plus) Functions (Canty)
class Functions for Classification
cluster Cluster Analysis Extended Rousseeuw et al.
codetools Code Analysis Tools for R
datasets The R Datasets Package
DBI R Database Interface
foreign Read Data Stored by Minitab, S, SAS,
SPSS, Stata, Systat, dBase, ...
graphics The R Graphics Package
Getting help
help(function_name)
help(prcomp)
?function_name
?prcomp
help.search(“topic”)
??topic or ??“topic”
Search CRAN
http://www.r-project.org
From R GUI: Help Search help…
CRAN Task Views (for individual packages)
http://cran.cnr.berkeley.edu/web/views/
R Useful Commands
• Commands can be expressions or assignments
• Separate by semicolon or new line
• Can split across multiple lines
• R will change prompt to + if command not finished
• Useful commands for variables
• ls(): List all stored variables
• rm(x): Delete one or more variables
• class(x): Describe what type of data a variable stores
• save(x,file=“filename”): Store variable(s) to a binary file
• load(“filename”): Load all variables from a binary file
• Save/load in current directory or My Documents by default
Variables in R Programming
> a = (1+1==3)
>a logical
[1] FALSE
Basic usage: arithmetic in R
• You can use R as a calculator
• Typed expressions will be evaluated and printed out
• Main operations: +, -, *, /, ^
• Obeys order of operations
• Use parentheses to group expressions
• More complex operations appear as functions
• sqrt(2)
• sin(pi/4), cos(pi/4), tan(pi/4), asin(1), acos(1), atan(1)
• exp(1), log(2), log10(10)
Data type
R provides the class() and typeof() functions to
find out what is the class and type of any variable.
R has five data types which are:
1. Numeric
2. Integers
3. Complex
4. Logical
5. Characters
For a variable to be valid, it
should follow these rules
• It should contain letters, numbers, and only dot or
underscore characters.
• It should not start with a number (eg:- 2iota)
• It should not start with a dot followed by a number
(eg:- .2iota)
• It should not start with an underscore (eg:- _iota)
Reserved Keywords in R
Useful Functions
length(object) # number of elements or components
str(object) # structure of an object
class(object) # class or type of an object
names(object) # names
c(object,object,...) # combine objects into a vector
cbind(object, object, ...) # combine objects as columns
rbind(object, object, ...) # combine objects as rows
ls() # list current objects
rm(object) # delete an object
newobject <- edit(object) # edit copy and save a newobject
fix(object) # edit in place
operator
An operator is a symbol that tells the compiler to
perform specific mathematical or logical
manipulations.
• Arithmetic Operators
• Relational Operators
• Logical Operators
• Assignment Operators
• Miscellaneous Operators
Arithmetic Operators