SHARE
TWEET

Untitled

a guest Jul 18th, 2019 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. set.seed(1)
  2.  df1 <- data.frame(matrix(nrow=20,ncol=3))
  3.  colnames(df1) <- c("group","trial","hour")
  4.  df1$group <- rep(c("a","b"),each=10)
  5.  df1$trial <- rep(c(1,1,1,1,1,2,2,2,2,2),times=2)
  6.  df1$hour <- rep(c(1,11,21,31,41),times=4)
  7.  
  8.  
  9.  df2 <- data.frame(matrix(nrow=4,ncol=3))
  10.  colnames(df2) <- c("group","trial","end")
  11.  df2$group <- c("a","a","b","b")
  12.  df2$trial <- c(1,2,1,2)
  13.  df2$end <- runif(4,1,40)
  14.      
  15. df2 %>%
  16.    left_join(df1,by=c("group","trial")) %>%
  17.    group_by(group,trial) %>%
  18.    filter(hour<= end)
  19.      
  20. df2 %>%
  21.    left_join(df1,by=c("group","trial")) %>%
  22.    group_by(group,trial) %>%
  23.    slice(1:n(hour<= end)+1)`
  24.      
  25. library(dplyr)
  26. df2 %>%
  27.   left_join(df1,by=c("group","trial")) %>%
  28.   group_by(group,trial) %>%
  29.   slice({i1 <- which(hour <= end)
  30.         unique(c(i1, pmin(tail(i1, 1) + 1), n()))})
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top