owned this note
owned this note
Published
Linked with GitHub
# INBO CODING CLUB
28 May 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 |
Hans Van Calster |
Patrik Oosterlynck|
Dirk Maes |
Salva Fernandez (VLIZ) | **
Ruben Perez Perez |
Britt Lonneville (VLIZ) |*
Pieterjan Verhelst |*
Anja Leyman |***
Karen Cox |**
Joost Vanoverbeke |
Anneleen Rutten |
No yellow sticky notes online. Put your name + " | " and add a "*" each time you solve a challenge.
## Challenge 1
Damiano's solution:
Anja:
for (i in 1:ncol(swiss)){
j <- mean(swiss[,i])
print(j)
}
for (i in 1:ncol(iris)){
j <- typeof(iris[,i])
print(j)
}
for (i in seq_along(iris)){
j <- length(unique(iris[[i]]))
print(j)
}
distr <- c(-10, 0, 10, 100)
for (i in distr){
j <- rnorm(n = 10, mean = i)
print(j)
}
#### Salva's solution (using sapply instead of for loops)
```
# Compute the mean of every column in swiss.
sapply(swiss, FUN = mean)
# Determine the type of each column in iris.
sapply(iris, FUN = class)
# Compute the number of unique values in each column of iris.
sapply(
iris,
FUN = function(x){length(unique(x))}
)
#Generate 10 random normals (rnorm(n = 10)) from distributions with means of -10, 0, 10, and 100.
means <- c(-10, 0, 10, 100)
rnorm_10 <- function(x){rnorm(10, x)}
sapply(
means,
rnorm_10
)
```
#### Pieterjan's solution on unique values using n_distinct() (from the tidyverse package) instead of unique()
```
output <- vector("double", ncol(iris))
for (i in seq_along(iris)) {
output[[i]] <- n_distinct(iris[[i]])
}
output
```
## Challenge 2
### Karen's solution:
years <- seq.int(2010, 2019)
for (year in years) {
year_births_plot <-
births %>%
filter(year == year) %>%
ggplot() +
geom_col(mapping= aes(x = month, y = n_births)) +
ylab("Number of births") +
coord_flip() +
ggtitle(paste0("Number of births", year))
year_births_plot + ggsave(filename = paste0("./n_births_belgium_", year, ".png"), year_births_plot, device = "png")
}
#### Salva's solution:
```
for (i in unique(births$year)){
# Define number of the year for the file name and plot title
year <- as.character(i)
# Repeat plot
year_births_plot <-
births %>%
filter(year == i) %>%
ggplot() +
geom_col(mapping = aes(x = month, y = n_births)) +
ylab("Number of births") +
coord_flip() +
ggtitle(paste0("Number of births - ", year))
year_births_plot + ggsave(filename = file.path(".","data","20200528", paste0("n_births_belgium_", year, ".png")),
year_births_plot,
device = "png")
# Remove year variable from environment
rm(year)
}
```
## Intermezzo - Dice game
*Salva*: I have added a bit of interactivity to the game :)
```
result <- 0
n <- 0
while(result < 10){
n <- n + 1
dice <- sample(1:6, size = 2, replace = TRUE)
result <- sum(dice)
print(paste0("Game number: ", n, "- Result ", result))
if(result >= 10){
print("finish!")
} else{
question <- readline("Another try? :) Y/N: ")
if(question != "Y" & question != "N"){
question <- readline("Kan jij dat herhalen? Y/N: ")
}
if(question == "Y"){
} else if(question == "N"){
result <- 10
print("Ok bye bye!")
}
}
}
```
## Challenge 3
### Anja (simpele benadering ...)
#### part 1
```
yr <- 0 # max. 10 jaar, dan eindigt spel
dead <- FALSE
while(dead == FALSE){
yr <- yr + 1 # year number
food <- sample(foods, size = 1)
enemy <- sample(enemies, size = 1)
reproduction <- sample(reproductions, size = 1)
dead <- ifelse((yr == 10 | food == "poison" | enemy != "safe" | reproduction == TRUE), TRUE, dead)
}
txt <- ifelse(reproduction == TRUE, "fox succeeded to reproduce", "fox died before reproducing")
print(txt)
```
### Joost
```
fox <- TRUE
offspring <- 0
while(fox) {
food <- sample(foods, size = 1)
enemy <- sample(enemies, size = 1)
reproduction <- sample(reproductions, size = 1)
if(reproduction)
offspring <- offspring + 1
if((food %in% c("starving", "poison")) | !(enemy == "safe"))
fox <- FALSE
}
offspring
```
#### part 2
```
foxes_reproduced <- 0
for (fox in 1:10){
yr <- 0
dead <- FALSE
while(dead == FALSE){
yr <- yr + 1 # year number
food <- sample(foods, size = 1)
enemy <- sample(enemies, size = 1)
reproduction <- sample(reproductions, size = 1)
dead <- ifelse((yr == 10 | food == "poison" | enemy != "safe" | reproduction == TRUE), TRUE, dead)
}
foxes_reproduced <- ifelse(reproduction == TRUE, foxes_reproduced + 1, foxes_reproduced)
}
print(paste("number of foxes that reproduced is ", foxes_reproduced, sep=""))
```
### Joost
```
popsize <- 10
foxes <- rep(TRUE, popsize)
offspring <- rep(0, popsize)
while(any(foxes)) {
food <- sample(foods, size = popsize)
enemy <- sample(enemies, size = popsize)
reproduction <- sample(reproductions, size = popsize)
offspring[foxes & reproduction] <- offspring[foxes & reproduction] + 1
foxes[(food %in% c("starving", "poison")) | !(enemy == "safe")] <- FALSE
}
offspring
```