1

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.

P Wernli
  • 21
  • 2
  • You should take a look at this answer: https://stackoverflow.com/questions/1660124/how-to-sum-a-variable-by-group There are non-loop solutions for this – Cole Sep 17 '19 at 09:46
  • @Cole the instructor wants us to practice writing loops – P Wernli Sep 17 '19 at 10:05
  • I'd suggest, instead of using `rbind()` using `merge()`. First you'd need to rename your mean columns, and you won't need `ag$species` anymore. Then filter when both mean columns are not > 0. – Nacho Glez Sep 17 '19 at 10:18
  • You are already using ```aggregate```, expanding it to include year and species wouldn't be a stretch. Regarding your question, yes, I think you would loop through the years and count the number of sites in common. Or, you could ```merge``` species1_new and the second one. – Cole Sep 17 '19 at 10:23

0 Answers0