I'm very new to R and trying to start organising my data set.

I'm working on observational data encoded from videos of behavioural experiments. The output is one single column with all the entries listed in temporal order, like this:

```
SD3
No string start
Open lid
End state
End trial
String start
Open lid
End state
End trial
String start
Touch knob A
Remove string
Open lid
End state
End trial
No string start
Open lid
End state
Clapping hands
End trial
SD4
String start
Touch knob B
Touch knob B
Touch knob B
Open lid
End state
End trial
No string start
Touch knob B
Touch knob A
Remove string
Remove string
Touch knob B
End trial
No string start
Open lid
End state
End trial
String start
Touch knob B
Touch knob A
Remove string
Touch knob B
End trial

```

This is the version with the spelled out labels, but of course these could be easily substitutes with single letters or numbers.

In order to analyse the data I would need to divide this column into different groups. I've done it in Excel, but it requires lots of time and some steps need to be done by hand. I would like to find a practical way of doing it in R but I haven't found one yet.

What I think I would need is a way to assign a new variable to each row, based on something that was preceeding/following that row.
Es.: all the rows following SD3 (the ID label) but before SD4, need to be assigned a "SD3" variable (to be later on grouped based on this).

More difficult for me to work on is the division of the column into smaller groups: each participant was given 4 trials. I would need to separate each trial (identifying it with a new variable / putting it into a separate column or other similar solutions so that it can be analysed separately later).

In the example above, I would need to have an output like this:

```
SD3
No string start -- SD3 -- trial1
Open lid -- SD3 -- trial1
End state -- SD3 -- trial1
End trial -- SD3 -- trial1
String start -- SD3 -- trial2
Open lid -- SD3 -- trial2
End state -- SD3 -- trial2
End trial -- SD3 -- trial2
String start -- SD3 -- trial3
Touch knob A -- SD3 -- trial3
Remove string -- SD3 -- trial3
Open lid -- SD3 -- trial3
End state -- SD3 -- trial3
End trial -- SD3 -- trial3
No string start -- SD3 -- trial4
Open lid -- SD3 -- trial4
End state -- SD3 -- trial4
Clapping hands -- SD3 -- trial4
End trial -- SD3 -- trial4
SD4
String start -- SD4 -- trial1
Touch knob B -- SD4 -- trial1
Touch knob B -- SD4 -- trial1
Touch knob B -- SD4 -- trial1
Open lid -- SD4 -- trial1
End state -- SD4 -- trial1
End trial -- SD4 -- trial1
No string start -- SD4 -- trial2
Touch knob B -- SD4 -- trial2
Touch knob A -- SD4 -- trial2
Remove string -- SD4 -- trial2
Remove string -- SD4 -- trial2
Touch knob B -- SD4 -- trial2
End trial -- SD4 -- trial2
No string start -- SD4 -- trial3
Open lid -- SD4 -- trial3
End state -- SD4 -- trial3
End trial -- SD4 -- trial3
String start -- SD4 -- trial4
Touch knob B -- SD4 -- trial4
Touch knob A -- SD4 -- trial4
Remove string -- SD4 -- trial4
Touch knob B -- SD4 -- trial4
End trial -- SD4 -- trial4

```