【问题标题】:How to repeat calculation on a subset of a database如何对数据库的子集重复计算
【发布时间】:2014-02-05 04:32:37
【问题描述】:

我有一个大数据框,其中包含许多横断面,对于每个横断面,我想计算每年的 cros (x) 和值 (y) 的截距。然后我想知道截距在不同年份是如何变化的。我知道如何计算截距,但是我有很多横断面,我必须重复很多次,我想更自动地做到这一点。

这就是我的数据的样子:

df

   transects year cros value
    10    1996   11    -3
    10    1996   12     5
    10    2005   11    -9
    10    2005   12    -3
    10    2010   11    -8
    10    2010   12    -8
    11    1996   11     7
    11    1996   12    -4
    11    2005   11    -6
    11    2005   12     9
    11    2010   11     6
    11    2010   12    17
    12    1996   14   -16
    12    1996   15   -17
    12    2005   14   -18
    12    2005   15   -11
    12    2010   14    16
    12    2010   15     7

所以我创建了一个函数,对数据集进行子集化并使用该子集进行一些计算。

这里是代码。我使用了lapply,因为我希望将代码的结果放入list。但是,lapply 可能不是解决此问题的正确函数。

 transect <- c(10, 11, 12)  

o <- lapply(1:length(transect), function(i) {
        s101 <- subset(df, along == transect[[i+1]])

# 我想为每个样带创建一个子集,并使用该子集进行多次计算。

#沙丘体积

# 这确保我有一个拦截,如果没有高于 3 的值

AUC96<-0 
 AUC05<-0
 AUC10<-0

 # Here I calculate the intercept for the different years.

     d96 <- subset(s101, (cros >= 3.00) & (year == 1996))
     AUC96<-sintegral(d96$cros,d96$value)$int
     lengthdune96 <- max(d96$value)-min(d96$value)
     AUC962 <- lengthdune96*8.00
     AUC96 <- AUC96 +AUC962

     d05 <- subset(s101, (cros >= 3.00) & (year == 2005))
     AUC05<-sintegral(d05$cros,d05$value)$int
     lengthdune05 <- max(d05$alti)-min(d05$value)
     AUC052 <- lengthdune05*8.00
     AUC05 <- AUC05 +AUC052

     d10 <- subset(s101, (cros >= 3.00) & (year == 2010))
     AUC10<-sintegral(d10$cros,d10$value)$int
     lengthdune10 <- max(d05$value)-min(d05$value)
     AUC102 <- lengthdune10*8.00
     AUC10 <- AUC10 +AUC102

     # Here the difference between the years
     dune96.05 <- AUC05-AUC96
     dune05.10 <- AUC10-AUC05

  c(transect[[i+1]], dune96.05, dune05.10)                              
   })
   out <- as.data.frame(do.call(rbind, o))

但是当我尝试这个时,我得到了这个错误

`Error in approx(x, fx, n = 2 * n.pts + 1) :
      need at least two non-NA values to interpolate`

这是我第一次尝试制作这样的功能,所以可能是我做错了。我希望你能帮助我。

编辑:

所以在我稍微改变了答案之后,因为那并没有完全解决。但是我仍然收到一条错误消息,我真的被卡住了。我也尝试了不同的方法来解决这个问题,例如查看 plyr 包,但是我仍然得到相同的错误问题:

这就是我的代码的样子:

test<-lapply(unique(df$transect),function(i){s101 <- subset(df,df$transect==i)
{

  AUC96<-0
  AUC05<-0
  AUC10<-0

 d96 <- subset(s101, (cros >= 3.00) & (year == 1996))
 AUC96<-sintegral(d96$cros,d96$value)$int
 lengthdune96 <- max(d96$value)-min(d96$value)
 AUC962 <- lengthdune96*8.00
 AUC96 <- AUC96 +AUC962

 d05 <- subset(s101, (cros >= 3.00) & (year == 2005))
 AUC05<-sintegral(d05$cros,d05$value)$int
 lengthdune05 <- max(d05$alti)-min(d05$value)
 AUC052 <- lengthdune05*8.00
 AUC05 <- AUC05 +AUC052

 d10 <- subset(s101, (cros >= 3.00) & (year == 2010))
 AUC10<-sintegral(d10$cros,d10$value)$int
 lengthdune10 <- max(d05$value)-min(d05$value)
 AUC102 <- lengthdune10*8.00
 AUC10 <- AUC10 +AUC102

 dune96.05 <- AUC05-AUC96
 dune05.10 <- AUC10-AUC05

}

c(i,dune96.05, dune05.10)    
})

但我仍然收到此错误消息:

`Error in approx(x, fx, n = 2 * n.pts + 1) :
  need at least two non-NA values to interpolate` 

我不确定我做错了什么,该功能应该像这样工作。我希望有人能帮助我。

【问题讨论】:

  • 没有理由重新发明轮子。看看包裹plyr。它提供了按主题轻松做事并结合结果的功能。
  • 如果你不做一个小玩具的例子,总是很难给出好的答案。您可以使用by() 来计算数据框中因子year 的水平。
  • 示例df不包含列alti
  • @SvenHohenstein 抱歉,我忘了更改 alti 表示值,我现在已经编辑了。

标签: r function apply lapply


【解决方案1】:

我发现您使用 lapply 存在两个问题。您将 transect 索引为列表(它是一个向量),并且不要将它(或 df)作为参数传递给 lapply 中的函数,因此子集没有运气。试试这样的:

lapply(unique(df$transect),function(i,df){s101 <- subset(df,transect==i)
,...
c(i, dune96.05, dune05.10)     
},df)

【讨论】:

  • 我在逗号后添加了大括号,但缺少。但是现在我没有得到任何结果,我运行代码并且没有任何反应。但是我想得到一个我可以使用的结果列表。
  • 发生了什么样的“什么都没有”?没有消息? out 呢,它不包含你的列表吗?
  • 哦,是的,我知道出了什么问题,我的变量名是错误的...但是我仍然收到错误消息:FUN(X[[1L]], ...) 中的错误:未使用参数 (list(cros = c(30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100, 105, 110, 115, 120, 125, 130, 135, 140, 145, 150, 155, 160, 165, 170, 175, 180, 185, 190, 195, 200 等。我不知道为什么我得到这个值,函数中没有使用变量 cros,我真的不知道如何解决它。
猜你喜欢
  • 2013-01-26
  • 2020-12-11
  • 2016-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-21
相关资源
最近更新 更多