我有一个包含时间数据的数据框,我希望将某些行标记为属于感兴趣的时段。例如:
time <- c(1,2,3,5,7,9,23,24,28,43,45)
action <- c("clap","blink","stare","stare","clap","stare",
"clap","blink","stare","clap","stare")
我想找到每个拍手中5秒内发生的事情,希望通过创建一个新列,每个新的拍手和拍后拍摄序列都有一个索引标记。
我是R的新手所以可能有一种方法可以帮助我找到答案,所以如果是这样的话我会道歉。
答案 0 :(得分:0)
我相信这可以做你想要的。请注意,我已经在data.frame
添加了一行,因为你提到你需要一个标识符来查看在拍手后5秒内是否发生了某些事情。
# Your data
time <- c(1,2,3,5,7,9,23,24,28,43,45)
action <- c("clap","blink","stare","stare","clap","stare",
"clap","blink","stare","clap","stare")
df = data.frame(time=time,action=action)
# Added a row that does not occur within 5 seconds of a clap for illustration purposes.
df = rbind(df,data.frame(time=100,action='stare'))
# Add the group number, based on the claps.
df = df %>% mutate(group=cumsum(action=='clap'))
# Check if action is within 5 seconds of a clap.
df = df %>%
group_by(group) %>%
mutate(within_5_seconds = ifelse(time-time[1]<=5,1,0)) %>%
as.data.table
输出:
time action group within_5_seconds
1: 1 clap 1 1
2: 2 blink 1 1
3: 3 stare 1 1
4: 5 stare 1 1
5: 7 clap 2 1
6: 9 stare 2 1
7: 23 clap 3 1
8: 24 blink 3 1
9: 28 stare 3 1
10: 43 clap 4 1
11: 45 stare 4 1
12: 100 stare 4 0