# INBO CODING CLUB 30 June 2020 Welcome! ## Share your code snippet If you want to share your code snippet, copy paste your snippet within a section of three backticks (```): As an **example**: ``` library(tidyverse) ``` (*you can copy paste this example and add your code further down*) ## Deelnemers Name | Challenges --- | --- Damiano Oldoni | * Sander Devisscher|* Anja Leyman|* Emma Cartuyvels|*** Els De Bie |** Karen Cox |** An Leyssen |* Hans Van Calster |* Tanja Milotic|* Luc De Bruyn| Dirk Maes| * Jeroen Vanden Borre |* Alexander Van Braeckel |* Wim Mertens |* Raïsa Carmen | *** Anneleen Rutten |* Joost Vanoverbeke | Jolien Goossens |*** Pieterjan Verhelst |** No yellow sticky notes online. Put your name + " | " and add a "*" each time you solve a challenge. # Living code introduction ```r height_giraffe <- runif(n = 5, min = 4.3, max = 5.7) # weight of 5 giraffes weight_giraffe <- rnorm(n = 5, mean = 1192, sd = 300) # data.frame with height and weight df_giraffe <- tibble(height = height_giraffe, weight = weight_giraffe) map(df_giraffe, mean) map(df_giraffe, median) # nicknames of 5 giraffes names_giraffe <- c("Damiano", "Dirk", "Emma", "Hans", "Joost") # List with height and weight of 5 giraffes info_giraffe <- transpose(df_giraffe) %>% set_names(names_giraffe) # Add specific fields to some giraffes info_giraffe$Dirk$subspecies <- "Giraffa camelopardalis subsp. antiquorum" info_giraffe$Emma$GPS_trackerID <- "AFG4953" map(info_giraffe, class) map(info_giraffe, length) ``` ## Challenge 1 Emma: ``` # Compute the mean of every column in swiss swiss class(swiss) str(swiss) map(swiss, mean) map_dbl(swiss, mean) # Determine the type of each column in iris iris class(iris) str(iris) map(iris, class) map_chr(iris, class) # Set swiss and iris as a named list of two data.frames and get the number of # rows of each data.frame dfs <- list(swiss = swiss, iris = iris) class(dfs) str(dfs) map(dfs, nrow) map_df(dfs, nrow) ``` ``` map_int(dfs, nrow) ``` Dirk: ``` as.data.frame(map(dfs, nrow)) ``` # Intermezzo: the map strategy ``` bmi <- function(weight, height) weight / ((height)^2) Joost <- info_giraffe$Joost bmi(Joost$weight, Joost$height) ~ bmi(.x$weight, .x$height) map(info_giraffe, ~ bmi(.x$weight, .x$height)) # Define function bmi_giraffe <- function(giraffe) bmi(giraffe$weight, giraffe$height) # Apply to all map(info_giraffe, bmi_giraffe) ``` ## Challenge 2 Emma: ``` # Get 10 random numbers from a normal distribution (`rnorm(n = 10)`) for each of # the mean values and return it as a list of 4: means <- c(-10, 0, 10, 20) map(means, rnorm, n=10) # Read sensor data sensors <- list("A1" = "A1", "G7" = "G7", "H4" = "H4") sensors read_sensor_data <- function(sensor) { path <- paste0("./data/20200630_sensor_", sensor, ".txt") df <- read_csv(path, na = "NA", col_types = cols(datetime = col_datetime(format = "%d/%m/%Y %H:%M:%S"))) summarize(df, minimum = min(datetime), maximum = max(datetime)) } # Get max and min datetime for all sensor data data.frame and return them as # data.frame. Hint: write a function containing # summarize(df, minimum = min(datetime), maximum = max(datetime)) map(sensors, read_sensor_data) # Get 10 random numbers from normal distributions with these combinations of # mean and standard deviations and returned it as a list of 4 means <- c(-10, 0, 10, 20) st_dev <- c(1, 3, 2, 1.5) map2(means, st_dev, rnorm, n=10) ``` Hans: ``` #tip: map_df heeft een .id argument map_df(sensors, read_sensor_data, .id = "sensor") ``` ## Challenge 3 Raïsa: ``` p<-penguins %>% group_by(species) %>% nest() # Add a column called `plot` containing ggplot objects based on the geometry # and aesthetics below: geom_point(aes(x = bill_length_mm , y = bill_depth_mm, colour = sex)) f<-function(x){ggplot(p$data[[x]]) + geom_point(aes(x = bill_length_mm , y = bill_depth_mm, colour = sex)) } p$plot<-map(1:3,f) ``` Emma: ``` # Group `penguins` data.frame by species and nest it as shown below: p <- penguins %>% group_by(species) %>% nest() # Add a column called `plot` containing ggplot objects based on the geometry # and aesthetics below: creat_plot <- function(df){ ggplot(df) + geom_point(aes(x = bill_length_mm , y = bill_depth_mm, colour = sex)) } p %>% mutate(plot = map(data, creat_plot)) ```