2013-02-05 110 views
0

我想有一个循环,会为我进行计算,并导出变量到一个新的数据帧(与识别信息一起)。新计算循环

我的数据是这样的:

每一个独特的采样点(唯一的)有(他们通过WAVE不同)与之相关联的4个数据点。

WAVE REFLECT REFEREN PLOT LOCAT COMCOMP  DATE UNIQUE 
1 679.9  119  0 1  1  1 11.16.12  1 
2 799.9  119  0 1  1  1 11.16.12  1 
3 899.8  117  0 1  1  1 11.16.12  1 
4 970.3  113  0 1  1  1 11.16.12  1 
5 679.9  914 31504 1  2  1 11.16.12  2 
6 799.9 1693 25194 1  2  1 11.16.12  2 

我想创建一个新的数据帧,将是这样的: 对于每一个独特的采样点,我想计算从2特定的“WAVE”三围“WBI”。

WBI      PLOT .... UNIQUE 
(WAVE==899.8/WAVE==970) 1    1 
(WAVE==899.8/WAVE==970) 1    2 
(WAVE==899.8/WAVE==970) 1    3 
+5

欢迎StackOverflow上。我想提醒你的是,虽然这里的人是有帮助的,他们很可能会问你,[你尝试过什么?](http://whathaveyoutried.com)提供您已经尝试将极大地帮助我们来帮助你的代码中的一些少量。 –

+0

您可能需要研究以下问题/答案(http://stackoverflow.com/questions/13475039/how-to-optimize-the-following-code-with-nested-while-loop-multicore-an-option) 。这可能会给你一些关于如何使用循环的见解。祝你好运! – Jochem

回答

0

视输入data.frame有可能是在效率方面更好的解决方案的尺寸,但以下应该工作确定为小型或中型的数据集,并且是一种简单:

out.unique = unique(input$UNIQUE); 

out.plot = sapply(out.unique,simplify=T,function(uq) { 
    # assuming that plot is simply the first PLOT of those belonging to that 
    # unique number. If not yo should change this. 
    subset(input,subset= UNIQUE == uq)$PLOT[1]; 
}); 

out.wbi = sapply(out.unique,simplify=T,function(uq) { 
    # not sure how you compose WBI but I assume that are the two last 
    # record with that unique number so it matches the first output of your example 
    uq.subset = subset(input,subset= UNIQUE == uq); 
    uq.nrow = nrow(uq.subset); 
    paste("(WAVE=",uq.subset$WAVE[uq.nrow-1],"/WAVE=",uq.subset$WAVE[uq.nrow],")",sep="") 
}); 

output = data.frame(WBI=out.wbi,PLOT=out.plot,UNIQUE=out.unique); 

如果输入的数据是大不过你可能想利用德事实记录似乎被“UNIQUE”进行排序;重复的数据。框架子设置将是昂贵的。也可以将两个快速通话组合成一个通话,但会使其更加麻烦,所以我放弃了这种方式。