如何使用R中的条件连续检查列

时间:2016-03-21 11:56:16

标签: r

我是R的初学者,我有一个大型数据集,其中一部分是:

public class FlashMessageAuthSuccessHandler extends
    SavedRequestAwareAuthenticationSuccessHandler {

@Value(${flashmessage.loginsuccesful})
private String flashMessage;

public void setFlashMessage(String flashMessage) {
      this.flashMessage = flashMessage;
}

@Override
public void onAuthenticationSuccess(HttpServletRequest request,
        HttpServletResponse response, Authentication authentication)
        throws ServletException, IOException {

    FlashMapManager flashMapManager = new SessionFlashMapManager();

    FlashMap fm = new FlashMap();
    fm.put(KeyConstants.FLASH_INFO_KEY, flashMessage);

    flashMapManager.saveOutputFlashMap(fm, request, response);

    setDefaultTargetUrl("/");

    super.onAuthenticationSuccess(request, response, authentication);
}
}
  1. 我想连续阅读3行并检查所有3行中是否有log6 $ Measure< = 5。

  2. 然后根据之前的条件添加另一列True或False。

  3. 预期产出:

     Hours<- c(1:14)   
     Measure<- c(5,4,5,5,4,5,5,6,7,6,5,4,5,5)
     log6<- data.frame (Hours, Measure)
    

    有什么建议吗?

1 个答案:

答案 0 :(得分:4)

我们可以使用rollapply

中的library(zoo)
library(zoo)
log6$Result <- rollapply(log6$Measure, 3, FUN= 
         function(x) all(x<=5), fill=NA, align="right")
log6$Result
#[1]    NA    NA  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE

@jogo

建议的变体
!!(rollapply(data=(log6$Measure < 6), width=3, FUN=prod, 
              by=1, fill=NA, align="right"))

和另一个:

rollapply(log6$Meassure < 6, 3, all, fill=NA, align="right")

或者来自@nicola

rollmax(log6$Measure,3, fill=NA, align="right") <=5