访问ddply中的外部数据 - R.

时间:2015-04-23 11:05:34

标签: r scope plyr

上下文

我有一个大型2D matrix MAT ),其中包含模型输出,rows对应timecolumns对应depthMTimevector time coordinate(一年中的几天)和Mdepth另一个Depth coordinates

现在我有一个较小的数据框,其中包含特定的coordinates,我想评估模型输出(它对应于观察结果)。它看起来像:

    name Nobs yday LowerDepth UpperDepth
1   SiO    1  267          1          0
2   SiO    2  267          3          1
3   SiO    3  267          5          3
4   SiO    4  267          7          5
5   SiO    5   27          1          0
6   SiO    6   27          3          1
7   SiO    7   27          5          3
8   SiO    8   27          7          5
9   SiO    9  104          1          0
10  SiO   10  104          3          1
11  SiO   11  104          5          3
12  SiO   12  104          7          5
13  SiO   13  174          1          0
14  SiO   14  174          3          1
15  SiO   15  174          5          3
16  SiO   16  174          7          5

现在我想使用ddply通过MATLowerDepth之间的平均值以及UpperDepth周围的特定时间窗口来获取yday的相应值 像这样

ddply(sta,.(name,Nobs),mutate,
         modval = mean(MAT[ which( abs(MTime-yday)<4),
                            which( MDepth>UpperDepth & MDepth<LowerDepth) ] ))

问题:问题MAT似乎在ddply内无法识别。 有没有办法从ddply函数中访问外部变量?

可能的答案

我找到了以下解决方案,但可能有一些更好的方法.. 问题,正如@Roland指出的那样是使用mutate。

ddply(sta,.(name,time),function(d.sub){
     d=cbind( d.sub,data.frame(
       modval=mean(Varmat[ which( abs(MTime-d.sub$yday)<4),
                           which( MDepth>d.sub$UpperDepth & MDepth<d.sub$LowerDepth)] ))   
     )
   }
   ) 

0 个答案:

没有答案
相关问题