Load necessary libraries
library(dplyr)
library(tidyverse)
Import dataset
Toomey_ebird<-read.csv("MBT_ebird.csv")
head(Toomey_ebird)
## X list_ID common_name scientific_name date time count duration
## 1 1 S40748758 Snow Goose Anser caerulescens 2017-11-26 10:28 AM 16 20
## 2 2 S33616660 Snow Goose Anser caerulescens 2017-01-12 07:00 AM 1 90
## 3 3 S33809874 Snow Goose Anser caerulescens 2017-01-20 04:26 PM 1 59
## 4 4 S35533959 Snow Goose Anser caerulescens 2017-03-30 07:05 AM 1 100
## 5 5 S35698031 Snow Goose Anser caerulescens 2017-04-04 07:00 AM 1 127
## 6 6 S35861224 Snow Goose Anser caerulescens 2017-04-10 06:06 PM 1 68
## location latitude longitude count_tot month year
## 1 US-MO 38.87193 -90.18439 369 11 2017
## 2 US-MO 38.63891 -90.28538 272 1 2017
## 3 US-MO 38.63891 -90.28538 188 1 2017
## 4 US-MO 38.63891 -90.28538 283 3 2017
## 5 US-MO 38.63891 -90.28538 369 4 2017
## 6 US-MO 38.63891 -90.28538 28 4 2017
#create function
bird_call<-function(sci_name){ #name function and variable to input into function
bird<-Toomey_ebird %>% #create a data frame with information from the ebird file
filter(scientific_name == sci_name) #filter out a specific scientific name
write_csv(bird,paste0(sci_name,".csv")) #write a file to a csv
}
#create list of all the things to put into the function
birds=c("Anser caerulescens", "Antrostomus carolinensis", "Setophaga americana")
#loop the list through the function
for (item in birds){
bird_call(item)
}
#create an empty dataframe
Most_and_least_df<-NULL
#create function
Most_and_least<-function(file_name){ #name function and input file
bird_count<-read.csv(paste0(file_name,".csv")) #import file
max_obs=slice_max(bird_count, count) #select the row with the most observations of the bird
head_max_obs=slice_head(max_obs) # just take the top row from the file with the max observations
min_obs=slice_min(bird_count, count) #select the row with the fewest observations of the bird
head_min_obs=slice_head(min_obs) #just take the top row from the file with the min observations
Most_and_least_df<-rbind(Most_and_least_df,head_max_obs, head_min_obs) #combine the files with the single min and max observations
assign('Most_and_least_df',Most_and_least_df,envir=.GlobalEnv) #put the dataframe in the global environment
}
#run the function for every item in the list
for (item in birds){ #loop through each species in the list
Most_and_least(item) #run the function
}
#export file
write_csv(Most_and_least_df,
("Most_and_Least.csv"))
head(Most_and_least_df)
## X list_ID common_name scientific_name date
## 1 14 S82037948 Snow Goose Anser caerulescens 2021-02-20
## 2 2 S33616660 Snow Goose Anser caerulescens 2017-01-12
## 3 872 S1665531 Chuck-will's-widow Antrostomus carolinensis 2004-04-22
## 4 872 S1665531 Chuck-will's-widow Antrostomus carolinensis 2004-04-22
## 5 5995 S18948766 Northern Parula Setophaga americana 2014-06-29
## 6 5979 S67802026 Northern Parula Setophaga americana 2020-04-25
## time count duration location latitude longitude count_tot month year
## 1 05:15 PM 26 4 US-OK 35.96645 -95.49374 696 2 2021
## 2 07:00 AM 1 90 US-MO 38.63891 -90.28538 272 1 2017
## 3 <NA> 1 0 US-FL 27.18182 -81.35875 15 4 2004
## 4 <NA> 1 0 US-FL 27.18182 -81.35875 15 4 2004
## 5 11:00 AM 6 360 US-MO 37.72918 -92.39330 101 6 2014
## 6 11:48 AM 1 140 US-OK 36.78372 -98.18573 64 4 2020
#create a new bird list
birds2=c("Branta canadensis", "Spatula discors", "Anas platyrhynchos")
#clear out the dataframe that was created above
Most_and_least_df<-NULL
#create a function with already created functions
fun_combine<-function(species){ #name new function
bird_call(species) #call first function
Most_and_least(species) #call second function
}
#loop through each item on the new bird list
for (item in birds2){
fun_combine(item)
}
#export file
write_csv(Most_and_least_df,
("Most_and_Least_problem3.csv"))
head(Most_and_least_df)
## X list_ID common_name scientific_name date time count
## 1 147 S82037948 Canada Goose Branta canadensis 2021-02-20 05:15 PM 200
## 2 24 S37097000 Canada Goose Branta canadensis 2017-05-23 02:06 PM 1
## 3 249 S74645100 Blue-winged Teal Spatula discors 2020-10-10 11:36 AM 60
## 4 222 S111728537 Blue-winged Teal Spatula discors 2022-05-30 08:08 AM 1
## 5 444 S82037948 Mallard Anas platyrhynchos 2021-02-20 05:15 PM 250
## 6 312 S54632771 Mallard Anas platyrhynchos 2019-04-06 11:20 AM 1
## duration location latitude longitude count_tot month year
## 1 4 US-OK 35.96645 -95.49374 696 2 2021
## 2 50 US-VT 44.09702 -73.34205 44 5 2017
## 3 7 US-OK 36.21568 -95.93332 219 10 2020
## 4 302 US-OK 36.85077 -102.88216 100 5 2022
## 5 4 US-OK 35.96645 -95.49374 696 2 2021
## 6 122 US-OK 36.20820 -96.06008 40 4 2019