[1] 2.4
(Sections 1.2-1.6)
R can be used like a calculator:
To get \(3^2 + 4^2\) try:
Cube-root of 64:
R has a special square-root function:
Write one line of R-code to find:
\[\sqrt{3^2 + 4^2}\]
<-
is the assignment operatora
to the value 10Now you have the 10 whenever you want:
Information about a creature in the Land of Oz:
paste()
paste()
is also a functionsep
stuck in betweensep=""
, nothing is in between… inside values:
## Error: unexpected numeric constant in "100 3"
\n
will make a new line:
paste("Hello, I am a ",
creatureType,
".\nMy name is ",
creatureName,
".\nI like to eat ",
creatureFood,
".",
sep = "")
[1] "Hello, I am a Munchkin.\nMy name is Boq.\nI like to eat corn."
Well, not when just printed!
cat()
it, InsteadIn order to re-use a useful bit of code, make a function out of it:
This defines a function called intro()
.
To use a function, call it:
Parameter | Argument |
---|---|
name |
“Frederick” |
type |
“Winkie” |
food |
“macaroni” |
You can skip the names of the parameters:
Hello, I am a Winkie.
My name is Frederick.
I like to eat macaroni.
But you must keep the arguments in the order of the parameters you want to assign them to.
Let’s attach this package:
Births78
is a data frame.Data Structure
A particular way of organizing information in a computer program so that it can be used efficiently.
date births wday year month day_of_year day_of_month day_of_week
1 1978-01-01 7701 Sun 1978 1 1 1 1
2 1978-01-02 7527 Mon 1978 1 2 2 2
3 1978-01-03 8825 Tue 1978 1 3 3 3
4 1978-01-04 8859 Wed 1978 1 4 4 4
5 1978-01-05 9043 Thu 1978 1 5 5 5
6 1978-01-06 9208 Fri 1978 1 6 6 6
7 1978-01-07 8084 Sat 1978 1 7 7 7
8 1978-01-08 7611 Sun 1978 1 8 8 1
9 1978-01-09 9172 Mon 1978 1 9 9 2
10 1978-01-10 9089 Tue 1978 1 10 10 3
Does the daily number of births vary with time of year?
## source: https://r-graph-gallery.com/network-interactive.html
# Libraries
library(igraph)
library(networkD3)
# create a dataset:
data <- data.frame(
from=c("A", "A", "B", "D", "C", "D", "E", "B", "C", "D", "K", "A", "M"),
to=c("B", "E", "F", "A", "C", "A", "B", "Z", "A", "C", "A", "B", "K")
)
# Plot
p <- simpleNetwork(data, height="400px", width="400px")
p
“All work and no play makes Jack a dull boy.”