使用子集数据在R中创建循环 [英] Creating for loops in R using subset data

查看:168
本文介绍了使用子集数据在R中创建循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我开始在R编程,并试图计算一个数据集的斜率。这是我的代码:

 斜率<  -  vector()
gdd.values< - length .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)
斜坡[i] < - lm(log(species.richness)〜log(volume))$ coefficients [2]
}

当我运行它时,斜率值保持为空。所有其他值都很好(没有其他空集)。如果你发现任何明显的错误,请告诉我。谢谢

解决方案

目前,您正在遍历唯一值的长度,而不是唯一值本身。所以,像@RobJensen所说的那样,为循环向量和迭代调整。因此,为什么一些或所有返回的值会导致丢失,因为由于不精确的过滤器,数据可能不包含任何行。

然而,考虑一个更简化的方法,通过使用经常被忽略和被忽略的 by()来按照需要的分组因子对数据集进行子集化,并将返回的列表绑定到一个向量中:

  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 (体积))$系数[2]
})

斜率< - do.call(c,coeff_list)


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

解决方案

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)

这篇关于使用子集数据在R中创建循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆