0

I recently started programming in R, and am trying to compute slopes for a data set. This is my code:

slopes<- vector()
gdd.values <- length(unique(data.gdd$GDD))
for (i in 1:gdd.values){
  subset.data <- data.gdd[which(data.gdd$GDD==i),]
  volume <- apply(subset.data[,4,6],1,prod)
  species.richness <- apply(subset.data[,7:59],1,sum)
  slopes[i] <- lm(log(species.richness) ~ log(volume))$coefficients[2]
}

When I run it the "slopes" value remains empty. All other values are fine (no other empty sets). Let me know if you find any obvious mistakes. Thanks

N. J.
  • 105
  • 6
  • 1
    Including a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) in your question will increase your chances of getting an answer. – Samuel Nov 05 '17 at 23:25
  • 2
    This is definitively not the good way to do what you want to do. Could you please provide the structure of your data. Thus we will be able to give you advices – JRR Nov 05 '17 at 23:31
  • 1
    Should be `gdd.values <- unique(data.gdd$GDD)` and `for(i in gdd.values)`. – IceCreamToucan Nov 06 '17 at 00:39

1 Answers1

0

Currently, you are iterating across the length of unique values and not unique values themselves. So, as @RobJensen comments, adjust the for loop vector and iteration. Hence, why some or all returned values result in missing as subset.data may contain no rows due to imprecise filter.

However, consider a more streamlined approach using the often underused and overlooked by() to subset dataset by needed grouping factor(s) and bind returned list into a vector:

coeff_list <- by(data.gdd, data.gdd$GDD, FUN=function(df) {
  volume <- apply(df[,4,6],1,prod)
  species.richness <- apply(df[,7:59],1,sum)
  lm(log(species.richness) ~ log(volume))$coefficients[2]
})

slopes <- do.call(c, coeff_list)
Parfait
  • 104,375
  • 17
  • 94
  • 125