I have a data set with the columns: Site, Year, Date, count, and SpeciesName. I had a homework question that stated: "In some years, counts were performed twice at each site. For those years, find the mean count at each site for each species (there are two species we're looking at), and plot those means instead of the raw counts." I wrote this code (plot code not included):
species1=subset(channel_islands,SpeciesName=="Paralabrax clathratus, adult")
species2=subset(channel_islands,SpeciesName=="Hypsypops rubicundus, adult")
#subsets data for each species
species1_new=data.frame(Site=numeric(0),count=numeric(0),year=numeric(0),species=character(0))
for(i in unique(species1$Year)){
year=subset(species1,Year==i)
ag=aggregate(count~Site,data=year,mean)
ag$year=i
ag$species="Paralabrax clathratus,adult"
species1_new=rbind(species1_new,ag)
}
species2_new=data.frame(Site=numeric(0),count=numeric(0),year=numeric(0),species=character(0))
for(i in unique(species2$Year)){
year=subset(species2,Year==i)
ag=aggregate(count~Site,data=year,mean)
ag$year=i
ag$species="Hypsypops rubicundus,adult"
species2_new=rbind(species2_new,ag)
}
final=rbind(species1_new,species2_new)
My next question states: "Furthermore, only draw the plot if the two species co-occur (i.e., both have abundance > 0) in at least 10 sites during that year. (We aren't interested in making comparisons in years when most of the data is zeros)" Am I correct in my thinking that I would need to use the same for loops I've written above, but now with conditional "if" statements for when the two species co-occur? Thank you for the help, I do not want to start working in the wrong direction if this is incorrect.