I have already checked the other questions with on this issue, but since the problem seems to be very specific they weren't helpful.
I have a dataframe like this (this is just a quick example, example data from dput() is provided below):
It contains the abundance for 11 species on several different sites per county (more than 1500 sites in over 400ish counties) for each year 2005-2011. for each site, in every county, every year, all species have been accounted for, so there is either an NA, or a number in abundance for every year. The number of sites varies per county.
I would like to run the following loop to put the abundance into several columns: It should create a linear model to calculate population trends over these years and put the output in an additional row. In the end I would like to have a trend for every species on every site over the years:
alldata_lm$slope_abundance_plot <- NA alldata_lm$p_slope_abundance_plot <- NA species <- unique(alldata_lm$species) sites <- unique(alldata_lm$site) for (i in (1:length(species))) { for (k in(1:length(sites))) { print(c(i,k)) lm1 <- lm(abundance ~ year, data = alldata_lm[alldata_lm$species == species[i] & alldata_lm$site == sites[k],], na.action=na.omit) alldata_lm$slope_abundance_plot[alldata_lm$species == species[i] & alldata_lm$site == sites[k]] <- coefficients(lm1)[2] if (nrow(coef(summary(lm1)))>1){ alldata_lm$p_slope_abundance_plot[alldata_lm$species == species[i] & alldata_lm$site == sites[k]] <- coef(summary(lm1))[2,4]} } }
However, when I do, it returns the following error:
The same loop works perfectly with a very similar dataframe, the only difference is that the current dataframe contains far more NA's.
Deleting the NA's prior to running the loop does not help. I get the error message no matter if there are any NA's in the abundance column or not. I think the error might occur somewhere else. The year column does never contain any missing values.
I'd greatly appreciate any help! Thanks
EXAMPLE DATA