2021年5月2日星期日

Manipulating data frames in R: Group items based on condition

I want to group together all events that happened during a specific time interval. The "Interval.Max" column shows the end time of the interval.

    `Interval.Max       Event.Time  

1 7.5 0.4 2 12.0 3.7 3 19.3 5.2 4 24.4 9.1 5 27.0 9.8 6 31.8 11.7 7 NA 12.9 8 NA 14.9 9 NA 17.0 10 NA 19.1 11 NA 19.4 12 NA 22.2 13 NA 23.7 14 NA 24.9 15 NA 25.7 16 NA 26.1 17 NA 26.8 18 NA 28.2 19 NA 29.5 20 NA 31.7`

Interval.Max Event.Time 1 7.5 0.4 2 12.0 3.7 3 19.3 5.2 4 24.4 9.1 5 27.0 9.8 6 31.8 11.7 7 NA 12.9 8 NA 14.9 9 NA 17.0 10 NA 19.1 11 NA 19.4 12 NA 22.2 13 NA 23.7 14 NA 24.9 15 NA 25.7 16 NA 26.1 17 NA 26.8 18 NA 28.2 19 NA 29.5 20 NA 31.7

My goal is to end up with something like this:

Interval.Max       Event.Time         Group  

1 7.5 0.4 1 2 12.0 3.7 1 3 19.3 5.2 1 4 24.4 9.1 2 5 27.0 9.8 2 6 31.8 11.7 2 7 NA 12.9 3 8 NA 14.9 3 9 NA 17.0 3 10 NA 19.1 3 11 NA 19.4 4 12 NA 22.2 4 13 NA 23.7 4 14 NA 24.9 5 15 NA 25.7 5 16 NA 26.1 5 17 NA 26.8 5 18 NA 28.2 6 19 NA 29.5 6 20 NA 31.7 6

or:

Interval.Max Event Onset Time 7.5 0.4 7.5 3.7 7.5 5.2 12 9.1 12 9.8 12 11.7 19.3 12.9 19.3 14.9 19.3 17 19.3 19.1 24.4 19.4 24.4 22.2 24.4 23.7 24.4 24.9 27 25.7 27 26.1 27 26.8 31.8 28.2 31.8 29.5 31.8 31.7

How do I do this in R?

Thanks!

https://stackoverflow.com/questions/67362403/manipulating-data-frames-in-r-group-items-based-on-condition May 03, 2021 at 08:58AM

没有评论:

发表评论