Mutating sequential behavior using a pipe

How would I be able to figure out how many times transitions to other behaviors in sequential df

Can you please share a small part of the data set in a copy-paste friendly format?

In case you don't know how to do it, there are many options, which include:

  1. If you have stored the data set in some R object, dput function is very handy.

  2. In case the data set is in a spreadsheet, check out the datapasta package. Take a look at this link.

I can include a dput for the data if required

Afraid not @hawken1.
In your first post you show that your data is in object named df.
For first 50 records we do

head(df, n=50)

to share in a forum post we need

dput(head(df, n=50))

Here is the output that I get when using dput

Here is what I tried to run since I figured I would have to first mutate and then filter the bheavior. Then I used group_by to summarize the subsequent events, but I am getting an error after the third line.

df2 <- df %>% mutate(next_event = lead(real_beh))

df3 <- df2 %>% filter(real_beh=="eat _ STOP")

df4 <- df3 %>% group_by(next_event)

what am I missing?

I named your structure above as df, then continued:

# for every row, get the next event after it
df2 <- df %>% mutate(next_event = lead(real_beh))
#we want to track what happens next after each eat and stop
df3 <- df2 %>% filter(real_beh=="eat _ STOP")
# count them
df4 <- df3 %>% group_by(next_event) %>%
# A tibble: 2 x 2
next_event      counts
<chr>            <int>
 eat _ START          2
 walking _ START      6

Thank you for your help.

You're welcome.
You can refer to the part of the code I shared which uses filter to keep only matching rows from the data. Its just a case of choosing a filter condition. Have a play with it.

the exclamation mark can be used to negate a logical condition so if you dont want something to match you can do

! thing_on_left == thing_on_right
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.