Attaching package: 'scales'
The following object is masked from 'package:purrr':
discard
The following object is masked from 'package:readr':
col_factor
Rows: 82 Columns: 4
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (1): country
dbl (3): capture, aquaculture, total
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Rows: 245 Columns: 2
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (2): country, continent
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
read_csv
Before we get started, I want to introduce read_csv.
read_csv reads comma delimited files. The first argument to read_csv() is the most important: it’s the path to the file to read. This function uses the first line of the data for the column names, which is a very common convention. The data might not have column names. You can use col_names = FALSE to tell read_csv() not to treat the first row as headings, and instead label them: read_csv(path, col_names = FALSE).
Another option that commonly needs tweaking is na: this specifies the value (or values) that are used to represent missing values in your file: read_csv(path, na = "NA")
Working with multiple data frames
Often instead of being provided the data you need for your analysis in a single data frame, you will need to bring information from multiple datasets together into a data frame yourself. These datasets will be linked to each other via a column (usually an identifier, something that links the two datasets together) that you can use to join them together.
There are many possible types of joins. All have the format something_join(x, y).
x<-tibble( value =c(1, 2, 3), xcol =c("x1", "x2", "x3"))y<-tibble( value =c(1, 2, 4), ycol =c("y1", "y2", "y4"))x
# A tibble: 3 × 2
value xcol
<dbl> <chr>
1 1 x1
2 2 x2
3 3 x3
y
# A tibble: 3 × 2
value ycol
<dbl> <chr>
1 1 y1
2 2 y2
3 4 y4
We will demonstrate each of the joins on these small, toy datasets.
Note: These functions below know to join x and y by value because each dataset has value as a column. See for yourself!
The Fisheries and Aquaculture Department of the Food and Agriculture Organization of the United Nations collects data on fisheries production of countries.
Goal: Our goal is to create a visualization of the mean share of aquaculture by continent.
Let’s start by looking at the fisheries data frame.
Demo: Take a look at the updated joined_fish data frame. There are some countries that were not in continents. In the first code chunk, identify which countries these are (they will have NA values for continent). In the second code chunk, manually update the continent information for these countries using the case_when function. Finally, check that these updates have been made as intended and no countries are left without continent information.
Comment through the following code below. Specifically…
– How is case_when being used? – Why quotes? – What does the ~ in words?
– What is TRUE doing?
joined_fish<-joined_fish|>mutate( continent =case_when(country=="Democratic Republic of the Congo"~"Africa",country=="Hong Kong"~"Asia",country=="Myanmar"~"Asia", TRUE~continent))
Demo: Add a new column to the joined_fish data frame called aq_prop. We will calculate it as aquaculture / total. Save the resulting frame as joined_fish.
Your turn (5 minutes): Now expand your calculations to also calculate the mean, minimum and maximum aquaculture proportion for continents in the fisheries data. Note that the functions for calculating minimum and maximum in R are min() and max() respectively.
# A tibble: 5 × 4
continent min_aq_prop max_aq_prop mean_aq_prop
<chr> <dbl> <dbl> <dbl>
1 Africa 0 0.803 0.0943
2 Americas 0 0.529 0.192
3 Asia 0 0.782 0.367
4 Europe 0.00682 0.618 0.165
5 Oceania 0.0197 0.357 0.150
———————– Solution to above answer below ————————-
Demo: Using your code above, create a new data frame called fisheries_summary that calculates minimum, mean, and maximum aquaculture proportion for each continent in the fisheries data.
Demo: Then, determine which continent has the largest value of max_ap. Take the fisheries_summary data frame and order the results in descending order of mean aquaculture proportion.