owned this note
owned this note
Published
Linked with GitHub
# INBO CODING CLUB
21 August, 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)
mijn_fantastische_code
...
```
(*you can copy paste this exampl
e and add your code further down, but do not fill in your code in this section*)
Your snippets:
## Challenge 1
### Stop uitvoering indien fout:
https://www.rdocumentation.org/packages/R.oo/versions/1.2.7/topics/trycatch
### Dirty old school solution: Dirk
```r
# for (file in files_in_dir) {
if (stringr::str_detect(file, "decay")) {
# read the data
concentrations <- readr::read_csv(file)
# make and print a plot of the data
plot_conc <- ggplot(concentrations, aes(x = concentrations[, 1], y = concentrations[, 2])) +
geom_point(size = 2) +
xlab("time (s)") + ylab("concentration (mg/l)") +
ggtitle(basename(file))
print(plot_conc)
}
}
```
### Rename first column: Lies/Sander
```r
for (file in files_in_dir) {
if (stringr::str_detect(file, "decay")) {
# read the data
concentrations <- readr::read_csv(file)
colnames(concentrations)[1] <- "time" # Rename first column
# make and print a plot of the data
plot_conc <- ggplot(concentrations) +
geom_point(aes(time, conc_data), size = 2) +
xlab("time (s)") + ylab("concentration (mg/l)") +
ggtitle(basename(file))
print(plot_conc)
}
}
```
### File opkuisen: Luc
CSV FILE opkuisen aja...
### Weg met de tibbles
Gebruik de gewone read.csv()
```r
concentrations <- read.csv(file)
```
Daarna de kolommen selecteren met vierkante haken
```r
x <- concentrations[,1]
y <- concentrations[,2]
```
### Rename: Floris
```r
for (file in files_in_dir) {
if (stringr::str_detect(file, "decay")) {
# read the data
concentrations <- readr::read_csv(file) %>%
rename(time = 1,
conc_data = 2)
# make and print a plot of the data
plot_conc <- ggplot(concentrations) +
geom_point(aes(time, conc_data), size = 2) +
xlab("time (s)") + ylab("concentration (mg/l)") +
ggtitle(basename(file))
print(plot_conc)
}
```
### Conditional rename: Peter
```r
if ("time_s" %in% colnames(concentrations)) {
concentrations <- concentrations %>%
rename(time = time_s)
}
```
### rename_at(): Joost/Peter
`dplyr:rename_at()` is wel wat voodoo, geen idee waarom tilde nodig is.
```r
concentrations <- concentrations %>% rename_at(1, ~ "time")
```
## Challenge 2
### Remove NA in sum(): Sander
```r
sum_squared_error <- function(data, model) {
return(sum((data - model)^2, na.rm = TRUE))
}
```
### En ook library laden! (floris)
```r
library(tidyverse)
```