All actions are performed via functions: sqrt
, mean
, help
, library
, t.test
, plot
Input: required and optional arguments; within parentheses, separated by comma
required: need to be supplied
optional: have default values
Beware of sequence of arguments; required ones come first. Compare
Some functions have special “argument”: ...
Meaning: anything that makes sense. Example: in c
and paste
function, which allow for the use of many values to
Output: result of calculations (typically assigned to R object), graphics, help window, etc.
You can use functions within other functions, e.g. mean(c(3,6,8))
The last value is returned as output - You can write your own functions:
Plot fare paid as function of age
Exercise 1: What would you like to change to make it good enough for publication?
dependent ~ independent
+
and *
)data
and subset
argumentSome possible changes: change labels along axes, rotate numbers along y-axis, add regression line, rescale fare
xlab
, ylab
, main
type
xlim
, ylim
, log
par
function
cex.axis
, cex.lab
, las
cex
, pch
col
, lty
, lwd
, cex
par
function
mar
, mfcol
, mfrow
plot
hist
boxplot
points
lines
, abline
text
, legend
, title
, arrows
, segments
Exerise 2. Make a histogram of age
Exercise 3.a. Make a boxplot of fare by passenger class.
Exercise 3.b. There are high values for fare that disturb the plot. Therefore, only show the fare up to 300 British Pounds.
or win.metafile("foo.wmf",...)
or png("foo.png",...)
and ending with dev.off()
Workspace: objects we create/import in RStudio: Global Environment in Environment pane
Packages with existing functions and data sets: base, stats, graphics
When a package is loaded in R session, a new environment is created
Listing of environments shown in RStudio via drop-down list under Global Environment in Environment pane
R | operating system |
---|---|
objects | files |
Workspace | current folder |
environments | folders in “path” variable |
Environment pane in RStudio | Explorer window |
Examples:
summary
:
summary.data.frame
, summary.factor
, summary.Date
default: summary.default
plot
: plot(y ~ x, data=..., ...)
Plot depends on class of `x`: scatterplot is default, but
boxplot if `x` is of class factor and plot by year if `x` is of class date
Why important to know?
plot
is a generic function, with specific methods depending on the class of the object that is the argument of the function.plot
Two components, with names teacher
and room
data.frame
Function help
. R is object oriented!
Search on Google (using language r:
)
CRAN (list with packages, Task Views, FAQs, Vignettes)
CRANsearcher in RStudio
Have a look at the links provided at the end of the handout
Find information on
apply
: apply a function over all columns or all rows of a data.frame
lapply
: apply a function over a list
sapply
: similar to lapply
but more user-friendly if output can be coerced into a vector
tapply
: can be used to split a vector in subgroups and apply a function to each of the subgroups
titanic
data set using sapply
and mode
tapply
function