---
title: "II\\. Working with data in R (solution)"
date: August 2025
author: "Data Science Lab, University of Copenhagen"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, message = FALSE, eval = TRUE)
```

Loading the core **tidyverse** packages, as well as the **readxl** package for importing data from .xlsx

```{r}
library(tidyverse)
library(readxl)
```

### Importing data

1. (and 2 and 3) Use the _Import data facility_. 

    We have already loaded the `readxl` package, so we can use the `read_excel()` function from that package.

    ```{r}
    climate <- read_excel("climate.xlsx")
    climate
    ```

### Working with the data

Below we will use functions from the `tidyverse` package, which was loaded using `library(tidyverse)` above.

4.  Trying out some basic commands.

    ```{r}
    # Only data from Oxford
    filter(climate, station == "oxford")
    # Only certain variables
    select(climate, station, year, month, af)
    # New variable with rain measured in cm
    mutate(climate, rain_cm = rain/10)
    # No of obs at each station
    count(climate, station)
    # Total sum over all stations
    summarize(climate, total_sun = sum(sun))
    # Sort after sun
    arrange(climate, sun)
    ```

5. Assigning the data set of observations from Oxford with 0 days of air frost to `oxford_af`.

    ```{r}
    oxford_af <- filter(climate, station == "oxford", af == 0)
    oxford_af
    ```
    
    Counting the number of observations assigned to `oxford_af`.
    
    ```{r}
    count(oxford_af)
    ```
    
    Alternative solution without using `oxford_af`. Preferably done using the pipe operator `%>%`.
    
    ```{r}
    climate %>% 
      filter(station == "oxford", af == 0) %>% 
      count()
    ```

6. Dataset with observations from Camborne and Oxford.

    ```{r, results = "hold"}
    filter(climate, station == "camborne" | station == "oxford")
    # Or: filter(climate, station %in% c("camborne", "oxford"))
    ```


7. Make the new variable and give the new dataset a name:

    ```{r}
    climate2 <- mutate(climate, sqrtSum=sqrt(sun))
    ```


8. Assigning the rainfall observations to the vector `rain_vector`.

    ```{r}
    rain_vector <- climate$rain
    ```
    
    Extracting elements from the rainfall vector. 
    
    ```{r}
    rain_vector
    # First six elements
    rain_vector[1:6]
    # Element number five
    rain_vector[5]
    # Element numbers 2, 4 and 6
    rain_vector[c(2,4,6)]
    ```
    
    Some summary statistics for the rainfall observations.
    
    ```{r}
    mean(rain_vector)
    sd(rain_vector)
    sum(rain_vector)
    ```

9. Summary statistics for the rainfall observations computed using `summarize`. It is most easily made with the pipe operator, but can also be done in several steps (not shown).

    ```{r}
    summarize(climate, 
          avg_rain = mean(rain),
          sd_rain = sd(rain),
          sum_rain = sum(rain))
    ```


10. Summary statistics for rainfall data by weather station, including number of observations for each station, and sorted in descending order according to annual rainfall.

    ```{r}
    climate %>%
      group_by(station) %>% 
      summarize(avg_rain = mean(rain),
                sd_rain = sd(rain),
                sum_rain = sum(rain),
                n=n()) %>% 
      arrange(desc(sum_rain))
    ```
   


11. Summary statistics for rainfall data by weather station, sorted in ascending order according to average monthly sunshine duration.

    ```{r}
    climate %>%
      group_by(station) %>% 
      summarize(avg_rain = mean(rain),
                sd_rain = sd(rain),
                sum_rain = sum(rain),
                avg_sun = mean(sun)) %>% 
      arrange(avg_sun) 
    ```
    
    
12. Weather station with largest median number of monthly sunshine hours over the months April to September was Oxford:

    ```{r}
    climate %>% 
      filter(month %in% 4:9) %>% 
      group_by(station) %>% 
      summarize(med_sun = median(sun)) %>% 
      arrange(desc(med_sun))
    ```

13. For each weather station apart from Armagh, the total rainfall (in cm) and duration of sunshine (in days) in the months with no days of air frost.

    ```{r}
    climate %>% 
      group_by(station) %>% 
      filter(station != "armagh", af == 0) %>% 
      summarize(total_rain = sum(rain)/10,
                total_sun = sum(sun)/24)
    ```

14. For each month: Number of stations with at least two days of air frost or more than 95 mm rain, and the average sunshine duration for these stations.
    
    ```{r}
    climate %>%
      filter(af >= 2 | rain > 95) %>% 
      group_by(month) %>% 
      summarize(count = n(), avg_sun = mean(sun))
    ```
    
15. Many solutions were already made with the pipe operator.

16. Computation of station-wise averages and standard deviations averages of both rain and sun:

    ```{r}
climate %>% group_by(station) %>% 
  summarize(across(c(rain,sun), list(avg=mean,sd=sd)))
    ```


**End of solution**
