Operators

+, -, /, …

Assignment operator: <-

Sequence operator: :

General data functions

ls(), rm(), c(), seq(), rep(), length(), sum(), na.exclude()

Statistical functions

mean(), sd(), var()

Playing with temperatures

Let’s define some temperature values:

> day <- c(24, 25, 26, 28, 29, 29, 30, 31, 31, 31, 29, 29)
> night <- c(27, 27, 26, 26, 26, 26, 26, 26, 25, 25, 25, 24)

Let’s check:

> ls()
[1] "day"   "night"

And the values:

> day
 [1] 24 25 26 28 29 29 30 31 31 31 29 29

and night:

> night
 [1] 27 27 26 26 26 26 26 26 25 25 25 24
> night <- c(27, 27, rep(26, 6), rep(25, 3), 24)
> length(day)
[1] 12
> length(night)
[1] 12
> temperature <- c(day, night)

Indexing variables

Extracting some values from a vector:

> temperature
 [1] 24 25 26 28 29 29 30 31 31 31 29 29 27 27 26 26 26 26 26 26 25 25 25
[24] 24
> temperature[5]
[1] 29
> temperature[-5]
 [1] 24 25 26 28 29 30 31 31 31 29 29 27 27 26 26 26 26 26 26 25 25 25 24
> temperature[c(1, 3, 5)]
[1] 24 26 29

Plots

Make a time vector from 7am to 6am (24 hour format).

> time <- c(7:23, 0:6) 

Make a plot with time and temperature and customize it in many ways.

> plot(time, temperature)

> plot(time, temperature, col = "red")

> plot(time, temperature, col = "red", pch = 19, cex = 3)

Hint for the plot below: use arguments pch = 21 and bg (stands for “background”):

> plot(time, temperature, col = "red", pch = 21, cex = 3, bg = "blue", lwd = 2)

> plot(time, temperature, type = "h")

> plot(time, temperature, type = "h", lwd = 5, col = "blue")

Hint for the plot below: use the function order() and reorder the time and temperature vectors. Also use the xlab and ylab argument of the plot() function.

> index <- order(time)
> time2 <- time[index]
> temperature2 <- temperature[index]
> plot(time2, temperature2, type = "l", lwd = 2, col = "green",
+      xlab = "time (hour)", ylab = "temperature (°C)")

Hint for the plot below: use the functions heat.colors() and rev().

> plot(time2, temperature2, col = rev(heat.colors(8))[temperature2 - 23], cex = 3,
+      pch = 19, xlab = "time", ylab = "temperature")

> plot(time2, temperature2, col = rev(heat.colors(8))[temperature2 - 23],
+      cex = temperature2 - 23, pch = 19, xlab = "time", ylab = "temperature",
+      ylim = c(24, 31.5))

Hints for the plot below: use the argument axes = FALSE and the functions axis(), seq_along() and paste().

> plot(temperature, axes = FALSE, pch = 17, cex = 2, xlab = "time")
> time3 <- time[seq(1, length(time), 2)]
> ltv <- length(time)
> axis(1, seq_along(time)[seq(1, ltv, 2)], paste(time)[seq(1, ltv, 2)])
> axis(2)

Hints for the plot below: use the rect(), the text() and the points() functions.

> plot(time, temperature, pch = 17, cex = 2, ylim = c(24, 32))
> rect(-2, 23, 6.5, 33, col = "black")
> rect(18.5, 23, 25, 33, col = "black")
> sel <- !(time > 6.5 & time < 18.5)
> points(time[sel], temperature[sel], pch = 17, cex = 2, col = "white")
> text(2.75, 31.75, "night", col = "white", font = 2)
> text(12.7, 31.75, "day", font = 2)
> text(21.3, 31.75, "night", col = "white", font = 2)
> box(bty = "o")