-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path07-analyzing_fisheries_data.Rmd
85 lines (67 loc) · 4.72 KB
/
07-analyzing_fisheries_data.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# Basic Fisheries Statistics
```{r include=FALSE}
knitr::opts_chunk$set(warning = F, message = F, error = F)
```
## Calculating Landings
One of the first analyses you may be interested in is calculating annual landings in the fishery. To calculate annual landings, take your `landings_data` data frame, add a column for weight of individual fish in kilograms by using the `mutate` function, group the data by year by using the `group_by` function, and then summarize the data for each year by summing the total weight of all fish caught in each year using the `summarize` and `sum` functions.
```{r}
# Start with the landings data frame
annual_landings <- landings_data %>%
# Add colomn for kilograms by dividing gram column by 1000
mutate(Weight_kg = Weight_g / 1000) %>%
# Group the data by year
group_by(Year) %>%
# Next, summarize the total annual landings per year
summarize(Annual_Landings_kg = sum(Weight_kg,na.rm=TRUE))
## Display a table of the annual landings data
annual_landings
```
Note the use of `na.rm = TRUE` in the code above. This is an important **argument** of many R functions (`sum()` in this case) and it tells R what to do with `NA` values in your data. Here, we are telling R to first remove `NA` values before calculating the sum of the `Weight_kg` variable. By default, many functions will return `NA` if **any** value is `NA`, which is often not desirable.
You may be interested in looking at landings across different gear types. Here, we now group the data frame by both the year and the gear type in order to summarize the total landings by year and by gear.
```{r}
# Start with the landings data frame
annual_gear_landings <- landings_data %>%
# Add colomn for kilograms by dividing gram column by 1000
mutate(Weight_kg = Weight_g / 1000) %>%
# Group the data by year and gear type
group_by(Year,Gear) %>%
# Next, summarize the total annual landings per year and gear type
summarize(Annual_Landings_kg = sum(Weight_kg,na.rm=TRUE))
## Display a table of the annual landings data by gear type
annual_gear_landings
```
## Calculating Catch-per-Unit-Effort (CPUE)
You may also be interested in calculating catch-per-unit-effort (CPUE). CPUE is calculated by dividing the catch of each fishing trip by the number of hours fished during that trip. This gives CPUE in units of kilograms per hour. The median for every year is then calculated in order to remove outliers - some fishers are much more efficient than others.
```{r}
# Start with the landings data frame
cpue_data <- landings_data %>%
# Add colomn for kilograms by dividing gram column by 1000
mutate(Weight_kg = Weight_g / 1000) %>%
# Group by year and Trip ID so that you can calculate CPUE for every trip in every year
group_by(Year,Trip_ID) %>%
# For each year and trip ID, calculate the CPUE for each trip by dividing the sum of the catch, converted from grams to kilograms, by the trip by the number of fishing hours
summarize(Trip_CPUE = sum(Weight_kg) / mean(Effort_Hours)) %>%
# Next, just group by year so we can calculate median CPUE for each year across all trips in the year
group_by(Year) %>%
# Calculate median CPUE for each year
summarize(Median_CPUE_kg_hour = median(Trip_CPUE))
# Display a table of the CPUE data
cpue_data
```
## Calculating Percent Mature
You may also wish to analyze your length data. One analysis would be to determine the percentage of mature fish in the catch in every year of the data frame. First let's define `m95`, the length at which 95% of fish are mature. For *Caesio cuning*, we know this is 15.9cm. Next, let's add a column to the data frame using the `mutate` function that represents whether each fish is mature or not (represented by a `TRUE` or `FALSE`), group the data frame by year, and then summarize for each year the percentage of mature fish out of the total number of sampled fish.
```{r}
# Define m95, the length at which 95% of fish are mature
m95 = 15.9
# Start with the landings data frame
landings_data %>%
# Add a column to the data that indicates whether each length measurement is from a mature or immature fish. If it's mature, this value should be TRUE; if immature, FALSE.
mutate(Mature = Length_cm > m95) %>%
# Group by year so we can see the percent mature for every year
group_by(Year) %>%
# The percentage mature is equal to the number of mature fish divided by the total number of fish and multiplied by 100
summarize(Percent_Mature = sum(Mature) / n() * 100)
```
Over 90% of the fish are mature throughout the time series, which is a great sign!
## Helpful Resources
+ [Data Wrangling with dplyr and tidyr Cheat Sheet](https://www.rstudio.com/wp-content/uploads/2015/02/data-wrangling-cheatsheet.pdf) is a very helpful resource for learning how to "wrangle" (manipulate) data in R