Skip to content

Commit

Permalink
markdown source builds
Browse files Browse the repository at this point in the history
Auto-generated via {sandpaper}
Source  : 0b0b7aa
Branch  : main
Author  : Claudiu Forgaci <[email protected]>
Time    : 2024-04-10 09:32:53 +0000
Message : Merge pull request #30 from carpentries-incubator/manuel/codestyle

Implement Tydeverse coding style on Lessons 1 - 12
  • Loading branch information
actions-user committed Apr 10, 2024
1 parent a2e603f commit 199823a
Show file tree
Hide file tree
Showing 10 changed files with 396 additions and 332 deletions.
26 changes: 14 additions & 12 deletions 01-intro-to-r.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ In the console type:


```r
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")
```

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: instructor
Expand Down Expand Up @@ -198,7 +198,7 @@ script and type:


```r
install.packages('here')
install.packages("here")
```

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

```r
# 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")
)
```

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: callout
Expand All @@ -320,9 +322,9 @@ You can use R as calculator, you can for example write:


```r
1+100
1*100
1/100
1 + 100
1 * 100
1 / 100
```


Expand All @@ -334,7 +336,7 @@ We using the assignment operator `<-`, like this:


```r
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 02-data-structures.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,11 +283,13 @@ You can reorder the categories using `factor()` function. This can be useful, fo

```r
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 @@ -308,12 +310,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 @@ -359,13 +361,13 @@ nordic_str

```r
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
```

```{.output}
Expand Down
42 changes: 21 additions & 21 deletions 03-explore-data.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ head(gapminder) # shows first 6 rows of the data set
```

```r
summary(gapminder) # basic statistical information about each column.
summary(gapminder) # basic statistical information about each column.
```

```{.output}
Expand Down Expand Up @@ -148,9 +148,9 @@ When you're analyzing a data set, you often need to access its specific columns.
One handy way to access a column is using it's name and a dollar sign `$`:

```r
# 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 @@ -171,7 +171,7 @@ First, we will adapt our data set, by keeping only the columns we're interested


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

head(year_country_gdp)
```
Expand All @@ -197,8 +197,8 @@ The `select()` statement with pipe would look like that:


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

head(year_country_gdp)
```
Expand All @@ -222,8 +222,8 @@ We already know how to select only the needed columns. But now, we also want to
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 @@ -250,9 +250,9 @@ Write a single command (which can span multiple lines and includes pipes) that w


```{.r .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 @@ -300,10 +300,10 @@ Calculate the average life expectancy per country. Which country has the longest

```{.r .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))
```

```{.output}
Expand Down Expand Up @@ -350,14 +350,14 @@ On top of this, you can also make multiple summaries of those groups:

```r
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()
)
)
```

## Frequencies
Expand All @@ -366,8 +366,8 @@ If you need only a number of observations per group, you can use the `count()` f

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

```{.output}
Expand All @@ -390,7 +390,7 @@ Frequently you’ll want to create new columns based on the values in existing c

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

head(gapminder_gdp)
```
Expand Down
Loading

0 comments on commit 199823a

Please sign in to comment.