我试图计算一个取决于多个其他列的值的变量,但是在其他行中。 这里的样本数据:确定组内是否有最近发生的事件
set.seed(2)
df1 <- data.frame(Participant=c(rep(1,5),rep(2,7),rep(3,10)),
Action=sample(c(rep("Play",9),rep("Other",13))),
time = c(sort(runif(5,1,100)),sort(runif(7,1,100)),sort(runif(10,1,100))))
df1$Action[2] ="Play" # edited to provide important test case
我想实现的是测试的最后一个“玩”事件是否是至多10秒前(时间列)的列。如果在过去10秒内没有“播放”事件,StillPlaying的值应为“n”,而不管当前操作如何。下面是我想什么,有一个样本:
Part Action time StillPlaying
1 1 Play 15.77544 n
2 1 Play 15.89964 y
3 1 Other 35.37995 n
4 1 Play 49.38855 n
5 1 Other 83.85203 n
6 2 Other 2.031038 n
7 2 Play 14.10483 n
8 2 Other 17.29958 y
9 2 Play 36.3492 n
10 2 Play 81.20902 n
11 2 Other 87.01724 y
12 2 Other 96.30176 n
http://stackoverflow.com/questions/3558988/basic-lag-in-r-vector-dataframe –
应该播放/其他操作分开考虑? – jalapic