Rbigpicture.ppt

advertisement
ST610-002
Intro to R programming
Big picture language features -- just a
brief description here -- more later:
Interpretive
R translates symbols (e.g. x, 2.53, *, qr) into
numbers, variables, and other expressions
which are evaluated immediately (almost)
Object-oriented
"everything is an object"
Well, that really doesn't say anything. But as I
see it, if a function takes a certain kind of
object -- as long as R can recognize it or force
into something it can recognize, then a
function will work -- and produce another
object, for another function or expression to
work with. Variables, constants, and functions
are all objects.
Coercion
R will instinctively try to make sense of any
expression that you give it, even if to you it is
nonsense. So this can be very dangerous, as R
will proceed many times when you would
prefer that it would stop processing for syntax
errors.
Recycling
When R is trying to coerce something to work,
and it needs more numbers, it will instinctively
reuse the numbers that it has. For example, if
you try to add a constant to a vector, R will
really take the constant and make a vector of
the same length as the other with it, by
repeating the single number that it has. While
very powerful, this can also be dangerous.
Lexical Scoping
The real power of R is in its functions, and R
creates a new working environment with each
instance of a function. Variables are
referenced (scoping) according its nested
environment structure.
Natural and unnatural looping
R is designed to do vector/matrix calculations
and works best when calculations are coded
to that end. R does have a loop feature (for
loop) which can be competitive in speed to its
natural looping, but more often is slow and
inefficient.
Some Details
•
•
•
•
•
•
•
gui vs batch and directory reference
source("infile.name")
sink("outfile.name") and sink() to discontinue
.RData and .Rhistory
help(command)
q() to stop
# begins a comment that continues to the end
of the record
Download