before I start here is a a small subset of the data I'm working with, i apologize in advance for it being so large (note this is only the first 30 rows of an extremely large dataset:
mydata<-structure(list(ParkName = c("SEP", "CSSP",
"SEP", "ONF", "SEP",
"ONF", "SEP",
"CSSP", "ONF",
"SEP", "CSSP",
"PPRSP", "PPRSP",
"SEP", "ONF",
"PPRSP", "ONF",
"SEP", "SEP",
"ONF"),
Year = c(2001, 2005, 1998,2011, 1991, 1991, 1991, 1991, 1991, 1992, 1992, 1992, 1992, 1992,
1992, 1992, 1992, 1993, 1994, 1994),
LatinName = c("Mola mola", "Clarias batrachus", "Lithobates catesbeianus", "Rana catesbeiana", "Rana catesbeiana",
"Rana yellowis", "Rana catesbeiana", "Solenopsis sp1","Rana catesbeiana", "Rana catesbeiana",
"Pratensis", "Rana catesbeiana", "Rana catesbeiana", "sp2", "Orchidaceae",
"Rana catesbeiana","Formica", "Rana catesbeiana", "Rana catesbeiana", "sp2"),
NumTotal = c(1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 100, 2, 1, 2)), Names = c("ParkName", "Year", "LatinName",
"NumTotal"),
row.names = c(NA, -20L), class = c("tbl_df", "tbl", "data.frame"))
This dataset represents the abundance of different species in different parks over a multitude of years. What I essentially want to do with this data is to get a species X park matrix for every year that data was recorded and then youse the 'vegan' package to calculate diversity indices for each park for each year. Obviously this is not a balanced dataset as not every park recorded species abundance for every year etc. Now I've realized to do this I need to run loops. I would need to get a list of parks per year and a list of species and their abundance per park per year in order to create these matrices. I'm not the greatest when it comes to running loops and this task is confusing me. For example, I created a separate vector of unique years in the dataset. I then created an empty list called "parkbyyear" to fill up with a list of parks by year from the main dataframe
year<-as.vector(unique(data[,3]))
parkbyyear<-NULL
for (i in 1:year) {
parkbyyear[i]<- mydata[mydata$ParkName[year == "i"]
}
The loop fails to run.
Any help would be appreciated.