owned this note
owned this note
Published
Linked with GitHub
# INBO CODING CLUB
20 September, 2018
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**:
```r
library(tidyverse)
...
```
(*you can copy paste this example and add your code further down, but do not fill in your code in this section*)
Your snippets:
### Examples of the created functions challenge 1
```r
#
```
```r
summary_decay <- function(data) {
# Get summary stats about concentration decay
result <- data %>%
summarize(min_decay = min(conc_data, na.rm = TRUE),
max_decay = max(conc_data, na.rm = TRUE),
t_min_decay = time[which.min(conc_data)],
d_50_time = time[which.min(abs(max(conc_data)/2 - conc_data))],
)
return(result)
}
```
```r
summary_decay <- function(data, conc = "conc_data", time = "time_s") {
stopifnot(require("assertthat"))
assert_that(inherits(data, "data.frame"))
assert_that(is.string(conc))
assert_that(is.string(time))
assert_that(has_name(data, conc))
assert_that(has_name(data, time))
stopifnot(require("dplyr"))
local_df <- data %>%
select(concentration = !!as.name(conc), times = !!as.name(time))
c_range <- local_df %>%
summarise_at("concentration", funs(min, max), na.rm = TRUE)
t_min <- local_df %>%
arrange(times) %>%
slice(1) %>%
select(t_min_decay = times)
t_d_50 <- local_df %>%
mutate(
delta = abs(max(concentration)/2 - concentration)
) %>%
arrange(delta) %>%
slice(1) %>%
select(d_50_time = times)
bind_cols(c_range, t_min, t_d_50)
}
```
```r
summary_decay <- function(data, conc = "conc_data", time = "time_s") {
stopifnot(require("assertthat"))
assert_that(inherits(data, "data.frame"))
assert_that(has_name(data, conc))
assert_that(has_name(data, time))
stopifnot(require("dplyr"))
c_range <- data %>%
summarise_at(
conc,
funs(min, max),
na.rm = TRUE
)
t_min <- data %>%
arrange(!!as.name(time)) %>%
slice(1) %>%
select(t_min_decay = !!as.name(time))
t_d_50 <- data %>%
mutate(
delta = abs(max(!!as.name(conc))/2 - !!as.name(conc))
) %>%
arrange(.data$delta) %>%
slice(1) %>%
select(d_50_time = !!as.name(time))
bind_cols(c_range, t_min, t_d_50)
}
```
```r
get_info_decay <- function(decay_data){
min <- min(decay_data$conc_data, na.rm = TRUE) # min
max <- max(decay_data$conc_data, na.rm = TRUE) # max
t_min <- decay_data$time[which.min(decay_data$conc_data)] # time to min
d_50 <- decay_data$time[which.min(abs(max(decay_data$conc_data)/2 - decay_data$conc_data))] # halveringstijd
info_decay <- data.frame(max, min, t_min, d_50, stringsAsFactors = FALSE)
return(info_decay)
}
```
```r
# ADD YOUR CODE HERE
# Franks (suggested) solution
t_min <- function(time, conc_data){
t_min_decay_1 <- decay_1$time[which.min(decay_1$conc_data)]
return(t_min_decay_1)
}
d_50 <- function(time, conc_data){
d_50_decay_1 <- decay_1$time[which.min(abs(max(decay_1$conc_data)/2 - decay_1$conc_data))]
return(d_50_decay_1)
}
info_decay_1 <- data.frame(min_decay = min(decay_1$conc_data, na.rm = TRUE),
t_min_decay = t_min(decay_1),
max_decay = max(decay_1$conc_data, na.rm = TRUE),
d_50_time = d_50(decay_1),
stringsAsFactors = FALSE)
```
```r
get_info_decay <- function(conc_data, time){
min_decay = min(conc_data, na.rm = T)
max_decay = max(conc_data, na.rm = T)
t_min_decay = time[which.min(conc_data)]
d_50_decay = time[which.min(abs(max(conc_data)/2 - conc_data))]
return(data.frame(min_decay = min_decay,
t_min_decay = t_min_decay,
max_decay = max_decay,
d_50_decay = d_50_decay,
stringsAsFactors = F))
}
```
###...
### Examples of the created functions challenge 2
```r sanders suggestion
decay_info <- function(temp_data, time_colname= "time", conc_colname = "conc_data"){
# Get summary stats about concentration decay
min_decay <- min(temp_data[[conc_colname]], na.rm = TRUE) # min
max_decay <- max(temp_data[[conc_colname]], na.rm = TRUE)
t_min_decay <- temp_data[[time_colname]][which.min(temp_data[[conc_colname]])] # time to min
# d-50
d_50_time <- temp_data[[time_colname]][which.min(abs(max(temp_data[[conc_colname]])/2 - temp_data[[conc_colname]]))]
# summarize these info in data frame (output)
info_decay_1 <- data.frame(min_decay ,
t_min_decay ,
max_decay ,
d_50_time ,
stringsAsFactors = FALSE)
return(info_decay_1)
}
```
```r
get_info_decay <- function(conc_data, time){
min_decay = min(conc_data, na.rm = T)
max_decay = max(conc_data, na.rm = T)
t_min_decay = time[which.min(conc_data)]
d_50_decay = time[which.min(abs(max(conc_data)/2 - conc_data))]
return(data.frame(min_decay = min_decay,
t_min_decay = t_min_decay,
max_decay = max_decay,
d_50_time = d_50_decay,
stringsAsFactors = F))
}
info_decay_2 <- get_info_decay(conc_data = decay_2$conc_data, time = decay_2$time_s)
````
```r
summary_decay <- function(data,
time_col_name = "time",
conc_col_name = "conc_data") {
# Get summary stats about concentration decay
data <- data.frame(time = data[[time_col_name]],
conc = data[[conc_col_name]])
result <- data %>%
summarise(min_decay = min(conc, na.rm = TRUE),
max_decay = max(conc, na.rm = TRUE),
t_min_decay = time[which.min(conc)],
d_50_time = time[which.min(abs(max(conc)/2 - conc))],
)
return(result)
}
```
```
p1<-ggplot(decay, aes(x = time, y = conc_data)) +
geom_point() +
geom_smooth() +
xlab("seconds") + ylab("concentration (mg/l)")
sovon_custum_theme(p1)
sovon_custum_theme<- function(p) {
p <- p+theme(axis.text.x = element_text(size = 15),
axis.title.x = element_text(size = 15, face = "bold"),
axis.text.y = element_text(size = 15),
axis.title.y = element_text(size = 15, face = "bold"))
return(p)
}
```