Create a data frame which consists of (copy and paste your R code for each of the following):
A variable “index” that runs from 1 to 100.
index <- c(1:100)
A variable “time” which runs from 0 to 4 with 100 elements. Hint: use the “length.out” argument of “seq()”.
time <- c(seq(from=0, to=4, length.out=100))
A variable “height” which is equal to 1.2(1+cos(2π×time)).
height <- 1.2*(1+cos(2*pi*time))
A variable “random” which is uniformly random between -1 and 1. Hint: use the “runif” function.
set.seed(88)
random <- runif(n=100,min=-1, max=1)
# set.seed() sets the initial starting point for random number generation and by that ensures reproducible results, for e.g. reporting.
# runif() requires the argument n which has no default. Because of the exercise's parameter to create a data frame with before mentioned variables that have the length of x=100 I determined n=100 for this exercise.
A variable “simulated” which is height + 0.5*random.
simulated <- height + 0.5*random
# Finally, after answering the five exercises above the data frame can be generated:
tidal_data <- data.frame(index,time,height,random,simulated)
Save this data frame to the file “tideheight.csv” using “write.csv”. Also plot the time variable against the height variable as a line in a time series plot, labelling the x-axis “time (days)” and the y-axis “Tide height (m)”. Add the values of “simulated” as dots to the plot by using “points()”.
write.csv(tidal_data,file="tideheight.csv")
plot(height~time,
type="l",
xlab="time (days)",
ylab="Tide height(m)")
points(simulated,pch=8)