1.1 Excercise

The 50 powers of 2 and all the integer numbers from 1 to 50

#Ex.1
powers_of_2 <- 2^(1:50)
squares <- (1:50)^2
matching_pairs <- which(powers_of_2 == squares)
matching_values <- cbind(matching_pairs, powers_of_2[matching_pairs])
knitr::kable(matching_values, col.names = c("n", "Value"))
n Value
2 4
4 16
print(matching_values)
##      matching_pairs   
## [1,]              2  4
## [2,]              4 16

1.2 Excercise

Angle functions calculated with different equations:

#Ex.2
x_values <- seq(0, 2*pi, by = 0.1)
sine_values <- sin(x_values)
cosine_values <- cos(x_values)
tangent_values_builtin <- tan(x_values)
tangent_values_manual <- sine_values / cosine_values

differences <- abs(tangent_values_builtin - tangent_values_manual)
equal_count <- sum(differences == 0)
largest_difference <- max(differences, na.rm = TRUE)

comparing tangent values

print(paste("Number of equal tangent values:", equal_count))
## [1] "Number of equal tangent values: 41"
print(paste("Largest difference between methods:", largest_difference))
## [1] "Largest difference between methods: 1.4210854715202e-14"

1.3 Excercise

a.) Here is an excerpt of the 1999 object:

#Ex.3
library(lfstat)
## Warning: package 'lfstat' was built under R version 4.2.3
## Loading required package: xts
## Warning: package 'xts' was built under R version 4.2.3
## Loading required package: zoo
## Warning: package 'zoo' was built under R version 4.2.3
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Loading required package: lmom
## Warning: package 'lmom' was built under R version 4.2.3
## Loading required package: lattice
data("ngaruroro", package = "lfstat")
str(ngaruroro)
## Classes 'lfobj' and 'data.frame':    13618 obs. of  6 variables:
##  $ day     : num  20 21 22 23 24 25 26 27 28 29 ...
##  $ month   : num  9 9 9 9 9 9 9 9 9 9 ...
##  $ year    : num  1963 1963 1963 1963 1963 ...
##  $ flow    : num  30.5 52.9 43.7 37.4 32.3 ...
##  $ hyear   : num  1964 1964 1964 1964 1964 ...
##  $ baseflow: num  NA NA NA NA NA NA NA NA NA NA ...
##  - attr(*, "lfobj")=List of 5
##   ..$ river      : chr "Ngaruroro"
##   ..$ station    : chr "Kuripapango"
##   ..$ unit       : chr "m^3/s"
##   ..$ institution: chr "National Institute of Water and Atmospheric Research (NIWA), New Zealand"
##   ..$ hyearstart : num 9
ngaruroro$Date <- as.Date(with(ngaruroro, paste(year, month, day, sep = "-")), format = "%Y-%m-%d")

flow_1999 <- subset(ngaruroro, format(ngaruroro$Date, "%Y") == "1999")


flow_1999 <- subset(ngaruroro, format(ngaruroro$Date, "%Y") == "1999")
nrow(flow_1999)
## [1] 365

Plot of the flow data of 1999

plot(flow_1999)

meanflow of 1999

mean_flow_1999 <- mean(flow_1999$flow, na.rm = TRUE)
print(paste("Mittlerer Abfluss des Jahres 1999:", round(mean_flow_1999, 2)))
## [1] "Mittlerer Abfluss des Jahres 1999: 13.97"

max flow of 1999

max_flow_day <- flow_1999[which.max(flow_1999$flow), ]

print(paste("Tag mit dem maximalen Abfluss im Jahr 1999:"))
## [1] "Tag mit dem maximalen Abfluss im Jahr 1999:"
print(paste("Datum:", max_flow_day$Date))
## [1] "Datum: 1999-11-11"
print(paste("Maximaler Abfluss:", round(max_flow_day$flow, 2), "m³/s"))
## [1] "Maximaler Abfluss: 135.82 m³/s"