Which plants are better for bees: native or non-native?
π Background
You work for the local government environment agency and have taken on a project about creating pollinator bee-friendly spaces. You can use both native and non-native plants to create these spaces and therefore need to ensure that you use the correct plants to optimize the environment for these bees.
The team has collected data on native and non-native plants and their effects on pollinator bees. Your task will be to analyze this data and provide recommendations on which plants create an optimized environment for pollinator bees.
πΎ The Data
You have assembled information on the plants and bees research in a file called plants_and_bees.csv. Each row represents a sample that was taken from a patch of land where the plant species were being studied.
| Column | Description |
|---|---|
sample_id | The ID number of the sample taken. |
species_num | The number of different bee species in the sample. |
date | Date the sample was taken. |
season | Season during sample collection ("early.season" or "late.season"). |
site | Name of collection site. |
native_or_non | Whether the sample was from a native or non-native plant. |
sampling | The sampling method. |
plant_species | The name of the plant species the sample was taken from. None indicates the sample was taken from the air. |
time | The time the sample was taken. |
bee_species | The bee species in the sample. |
sex | The gender of the bee species. |
specialized_on | The plant genus the bee species preferred. |
parasitic | Whether or not the bee is parasitic (0:no, 1:yes). |
nesting | The bees nesting method. |
status | The status of the bee species. |
nonnative_bee | Whether the bee species is native or not (0:no, 1:yes). |
Source (data has been modified)
πͺ Challenge
Provide your agency with a report that covers the following:
- Which plants are preferred by native vs non-native bee species?
- A visualization of the distribution of bee and plant species across one of the samples.
- Select the top three plant species you would recommend to the agency to support native bees.
π§ββοΈ Judging criteria
This is a community-based competition. The top 5 most upvoted entries will win.
The winners will receive DataCamp merchandise.
β
Checklist before publishing
- Rename your workspace to make it descriptive of your work. N.B. you should leave the notebook name as notebook.ipynb.
- Remove redundant cells like the judging criteria, so the workbook is focused on your work.
- Check that all the cells run without error.
βοΈ Time is ticking. Good luck!
suppressPackageStartupMessages(library(tidyverse))
data <- readr::read_csv("data/plants_and_bees.csv", show_col_types = FALSE)
data# check for structure of the data
str(data)We can see that at least specialized_on and status seem to have "null" values, i.e., missing values. Lets observe this further.
library(dplyr)
# for each column we want to check the unique values available
sapply(data, unique)Here we can see that not only specialized_on and status have missing values, but also the variables parasitic, nesting and nonnative_bee. Because there are not that many missing values, we could consider dropping observations with missing values. However, we will deal with them by imputing the missing value with the mode (this is possible due to the fact, that all of the variables are categorical variables).
# first, we have to convert 'null' values to actual NA values
# replace missing values in the data with the mode (i.e., the most frequent value in that variable)
calculate_mode <- function(var){
unique_vals <- unique(var)
table_vals <- tabulate(match(var, unique_vals))
unique_vals[table_vals == max(table_vals)]
}
#missing_cols <- c("specialized_on", "status", "parasitic", "nesting", "nonnative_bee")
print(sum(is.na(data)))
data[data == 'null'] <- NA
print(sum(is.na(data)))
#data[is.na(data)] <- "HAHAHA"
data <- data %>%
mutate(across(everything(), ~replace_na(.x, calculate_mode(.x))))# for each column we want to check the unique values available
sapply(data, unique)# create correct data types
# convert all character columns (if useful) to factors
cat_cols <- c("season", "site", "native_or_non", "sampling", "plant_species",
"bee_species", "sex", "parasitic", "nesting", "status", "nonnative_bee")
data <- data %>%
mutate(across(all_of(cat_cols), as.factor),
date = mdy(date))β
β