owned this note
owned this note
Published
Linked with GitHub
# INBO CODING CLUB
28 January 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**:
```
libraryG(tidyverse)
```
(*you can copy paste this example and add your code further down*)
### Deelnemers
1. Anik Schneiders
2. Arno Thomaes
3. Marc Esprit
4. Frank
5. Damiano
6. Kristof
7. Leen
8. David
9. Geert
10. Nico Noé
11. Bart Christiaens
12. Sam Provoost
13. Dirk
14. Jan VU
15. Sander Devisscher
16. Patrik
17. Erika
18. Ine
19. Katrijn
20. Helen
21. Bart
22. Sam
23. Emma
24. Wouter
25. Heidi
26. Marijke
### Challenge 1
Nico's answer
```
df <- butterflycounts_df # Less typing, I'm lazy
head(df)
str(df)
glimpse(df)
summary(df)
sprintf("There are %d rows and %d columns", ncol(df), nrow(df))
colnames(df)
View(df, "my butterfly dataframe")
```
Marijke oplossing
```
head(butterflycounts_df)
str(butterflycounts_df)
glimpse(butterflycounts_df)
summary(butterflycounts_df)
nrow(butterflycounts_df)
ncol(butterflycounts_df)
### of
dim(butterflycounts_df)
colnames(butterflycounts_df)
```
```
ggplot(fit_results) +
geom_line(aes(y = obs, x = year))+
facet_wrap(.~scientificName, nrow = 2, scales= 'free') +
geom_line(aes(x=year, y=fit),color='red', linetype='dotted')+
geom_ribbon(aes(ymin=lcl,ymax=ucl,x=year),alpha=0.3, fill='purple') + labs(title = 'Number of observations in Belgian Natura2000 protected areas',subtitle = 'Source: GBIF')+
theme(plot.title = element_text(hjust = 0),plot.subtitle = element_text(hjust = 0))
```
### Challenge 2
Nico's answer
```
distinct(df, year)
distinct(df, species)
distinct(df, year, month)
distinct(df, species, year)
# let's work with `summarise()`
summarize(df,
"n_distinct_garden"=n_distinct(id_garden),
"min_year"=min(year),
"max_year"=max(year),
"n_distinct_years"=n_distinct(year),
"min_counts"=min(n_individuals),
"max_counts"=max(n_individuals),
"mean_counts"=mean(n_individuals)
)
```
David's answer (with pipe)
```
butterflycounts_df %>%
summarise(n_distinct_gardens = n_distinct(id_garden),
min_year = min(year),
max_year = max(year),
n_distinct_years = n_distinct(year),
min_counts = min(n_individuals),
max_counts = max(n_individuals),
mean_counts = mean(n_individuals))
```
### Challenge 3
Nico's answer (not sure of the results)
```
# 1. summarise() as in challenge 2 but now let's do it for each species
group_by(df, species) %>% summarize(
"n_distinct_garden"=n_distinct(id_garden),
"min_year"=min(year),
"max_year"=max(year),
"n_distinct_years"=n_distinct(year),
"min_counts"=min(n_individuals),
"max_counts"=max(n_individuals),
"mean_counts"=mean(n_individuals)
)
# 2. Number of individuals per species (tot_species)
# I also sort them
group_by(df, species) %>% summarize(tot_species=sum(n_individuals)) %>% arrange(desc(tot_species))
# 3.
group_by(df, species, month) %>% summarize(tot_species=sum(n_individuals)) %>% arrange(desc(tot_species))
```
Geert answer
# 1. summarise() as in challenge 2 but now let's do it for each species
butterflycounts_df %>%
group_by(species) %>%
summarise(n_distinct_gardens = n_distinct(id_garden),
min_year = min(year),
max_year = max(year),
n_distinct_years = n_distinct(year),
min_counts = min(n_individuals),
max_counts = max(n_individuals),
mean_counts = mean(n_individuals))
butterflycounts_df %>%
group_by(year) %>%
summarise(n_distinct_gardens = n_distinct(id_garden),
min_year = min(year),
max_year = max(year),
n_distinct_years = n_distinct(year),
min_counts = min(n_individuals),
max_counts = max(n_individuals),
mean_counts = mean(n_individuals))
# 2. Number of individuals per species (tot_species)
tot_species <-butterflycounts_df %>%
group_by(species) %>%
summarise(total_counts = sum(n_individuals))
# 3. Number of individuals per species and month (tot_species_month)
tot_species_month <-butterflycounts_df %>%
group_by(species, month) %>%
summarise(total = sum(n_individuals))