Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Tydeverse coding style on Lessons 1 - 12 #30

Merged
merged 3 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions episodes/01-intro-to-r.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@ An alternative solution is to create the folders using R command `dir.create()`.
In the console type:

```{r create-directories}
dir.create('data')
dir.create('data_output')
dir.create('documents')
dir.create('fig_output')
dir.create('scripts')
dir.create("data")
dir.create("data_output")
dir.create("documents")
dir.create("fig_output")
dir.create("scripts")

```

Expand Down Expand Up @@ -218,7 +218,7 @@ We will however need to install the `here` package. To do so, please go to your
script and type:

```{r install-here-package, eval=FALSE}
install.packages('here')
install.packages("here")
```

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: callout
Expand Down Expand Up @@ -311,8 +311,10 @@ In the script, we will write:

```{r download-files}
# Download the data
download.file('https://bit.ly/geospatial_data',
here('data','gapminder_data.csv'))
download.file(
"https://bit.ly/geospatial_data",
here("data", "gapminder_data.csv")
)

```

Expand All @@ -337,9 +339,9 @@ will not cover these in the workshop.
You can use R as calculator, you can for example write:

```{r calculator}
1+100
1*100
1/100
1 + 100
1 * 100
1 / 100

```

Expand All @@ -351,7 +353,7 @@ use them whenever we need to.
We using the assignment operator `<-`, like this:

```{r asignment-operator}
x <- 1/40
x <- 1 / 40
```

Notice that assignment does not print a value. Instead, we've stored it for later
Expand Down
34 changes: 18 additions & 16 deletions episodes/02-data-structures.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,13 @@ You can reorder the categories using `factor()` function. This can be useful, fo

```{r factor-reorder1}
nordic_cat <- factor(
nordic_cat, levels = c(
'Norway',
'Denmark',
'Sweden'
))
nordic_cat,
levels = c(
"Norway",
"Denmark",
"Sweden"
)
)

# now Norway will be the first category, Denmark second and Sweden third
nordic_cat
Expand All @@ -230,12 +232,12 @@ we will use `fct_relevel()` function from `forcats` package to do the reordering
library(forcats)

nordic_cat <- fct_relevel(
nordic_cat,
'Norway' ,
'Denmark',
'Sweden'
) # With this, Norway will be first category,
# Denmark second and Sweden third
nordic_cat,
"Norway",
"Denmark",
"Sweden"
) # With this, Norway will be first category,
# Denmark second and Sweden third

nordic_cat
```
Expand Down Expand Up @@ -263,13 +265,13 @@ outside of this set, it will become an unknown/missing value detonated by
```{r factor-missing-level}
nordic_str
nordic_cat2 <- factor(
nordic_str,
levels = c('Norway', 'Denmark')
)
nordic_str,
levels = c("Norway", "Denmark")
)

# because we did not include Sweden in the list of
# because we did not include Sweden in the list of
# factor levels, it has become NA.
nordic_cat2
nordic_cat2
```
::::::::::::::::::::::::::::::::::::::::::::::::::::

Expand Down
42 changes: 21 additions & 21 deletions episodes/03-explore-data.Rmd
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
---

Check warning on line 1 in episodes/03-explore-data.Rmd

View workflow job for this annotation

GitHub Actions / Build markdown source files if valid

[unknown div] alert

Check warning on line 1 in episodes/03-explore-data.Rmd

View workflow job for this annotation

GitHub Actions / Build markdown source files if valid

[unknown div] alert
title: 'Exploring Data Frames & Data frame Manipulation with dplyr '
teaching: 10
exercises: 2
Expand Down Expand Up @@ -95,7 +95,7 @@

head(gapminder) # shows first 6 rows of the data set

summary(gapminder) # basic statistical information about each column.
summary(gapminder) # basic statistical information about each column.
# Information format differes by data type.

nrow(gapminder) # returns number of rows in a dataset
Expand All @@ -110,9 +110,9 @@

One handy way to access a column is using it's name and a dollar sign `$`:
```{r subset-dollar-sign}
# This notation means: From dataset gapminder, give me column country. You can
# see that the column accessed in this way is just a vector of characters.
country_vec <- gapminder$country
# This notation means: From dataset gapminder, give me column country. You can
# see that the column accessed in this way is just a vector of characters.
country_vec <- gapminder$country

head(country_vec)

Expand All @@ -128,7 +128,7 @@
First, we will adapt our data set, by keeping only the columns we're interested in, using the `select()` function from the `dplyr` package:

```{r dplyr-select}
year_country_gdp <- select(gapminder, year, country, gdpPercap)
year_country_gdp <- select(gapminder, year, country, gdpPercap)

head(year_country_gdp)

Expand All @@ -145,8 +145,8 @@

```{r dplyr-pipe}

year_country_gdp <- gapminder %>%
select(year,country,gdpPercap)
year_country_gdp <- gapminder %>%
select(year, country, gdpPercap)

head(year_country_gdp)

Expand All @@ -160,8 +160,8 @@

In the `gapminder` data set, we want to see the results from outside of Europe for the 21st century.
```{r}
year_country_gdp_euro <- gapminder %>%
filter(continent != "Europe" & year >= 2000) %>%
year_country_gdp_euro <- gapminder %>%
filter(continent != "Europe" & year >= 2000) %>%
select(year, country, gdpPercap)
# '&' operator (AND) - both conditions must be met

Expand All @@ -181,9 +181,9 @@


```{r ex5, class.source="bg-info"}
year_country_gdp_eurasia <- gapminder %>%
filter(continent == "Europe" | continent == "Asia") %>%
select(year, country, gdpPercap)
year_country_gdp_eurasia <- gapminder %>%
filter(continent == "Europe" | continent == "Asia") %>%
select(year, country, gdpPercap)
# '|' operator (OR) - one of the conditions must be met

nrow(year_country_gdp_eurasia)
Expand Down Expand Up @@ -215,10 +215,10 @@

```{r ex6 , class.source="bg-info"}
gapminder %>%
group_by(country) %>%
summarize(avg_lifeExp=mean(lifeExp)) %>%
filter(avg_lifeExp == min(avg_lifeExp) |
avg_lifeExp == max(avg_lifeExp) )
group_by(country) %>%
summarize(avg_lifeExp = mean(lifeExp)) %>%
filter(avg_lifeExp == min(avg_lifeExp) |
avg_lifeExp == max(avg_lifeExp))
```

### Multiple groups and summary variables
Expand All @@ -235,14 +235,14 @@
On top of this, you can also make multiple summaries of those groups:
```{r dplyr-summ}
gdp_pop_bycontinents_byyear <- gapminder %>%
group_by(continent,year) %>%
group_by(continent, year) %>%
summarize(
avg_gdpPercap = mean(gdpPercap),
sd_gdpPercap = sd(gdpPercap),
avg_pop = mean(pop),
sd_pop = sd(pop),
n_obs = n()
)
)

```

Expand All @@ -252,8 +252,8 @@
```{r dplyr-count}

gapminder %>%
group_by(continent) %>%
count()
group_by(continent) %>%
count()
```


Expand All @@ -263,7 +263,7 @@

```{r dplyr-mutate}
gapminder_gdp <- gapminder %>%
mutate(gdpBillion = gdpPercap*pop/10^9)
mutate(gdpBillion = gdpPercap * pop / 10^9)

head(gapminder_gdp)

Expand Down
Loading
Loading