How to figure out the number of shifts occurs within a column

How would I be able to figure out how many times transitions to other behaviors shown in the real_beh column in the image.

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.

Sorry I am new to R -- here is a copy and paste of a portion of the dataset? Is this okay?

    TIME behavior status         real_beh

20 369.672 walking START walking _ START
21 372.672 walking STOP walking _ STOP
22 2253.284 walking START walking _ START
23 2263.280 groom START groom _ START
24 2263.280 walking STOP walking _ STOP
25 2265.282 groom STOP groom _ STOP
26 2266.031 walking START walking _ START
27 2269.281 walking STOP walking _ STOP
28 2272.780 walking START walking _ START
29 2276.281 walking STOP walking _ STOP
30 2277.031 eat START eat _ START
31 2294.033 eat STOP eat _ STOP
32 2296.281 walking START walking _ START
33 2301.532 walking STOP walking _ STOP
34 2302.033 eat START eat _ START
35 2439.999 eat STOP eat _ STOP
36 2442.500 eat START eat _ START
37 2451.249 eat STOP eat _ STOP
38 2451.750 walking START walking _ START
39 2459.248 walking STOP walking _ STOP
40 2459.249 eat START eat _ START
41 2462.000 eat STOP eat _ STOP
42 2464.498 walking START walking _ START
43 2464.999 walking STOP walking _ STOP
44 2465.501 eat START eat _ START
45 2516.583 eat STOP eat _ STOP
46 2517.082 walking START walking _ START
47 2521.332 walking STOP walking _ STOP
48 2521.582 groom START groom _ START

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:

dput(head(df, n=50))
structure(list(TIME = structure(c(205L, 209L, 2L, 3L, 3L, 4L,
5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L,
19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L,
32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L,
45L, 46L, 47L, 48L), .Label = c("", "2253.284", "2263.280", "2265.282",
"2266.031", "2269.281", "2272.780", "2276.281", "2277.031", "2294.033",
"2296.281", "2301.532", "2302.033", "2439.999", "2442.500", "2451.249",
"2451.750", "2459.248", "2459.249", "2462.000", "2464.498", "2464.999",
"2465.501", "2516.583", "2517.082", "2521.332", "2521.582", "2525.330",
"2525.832", "2533.082", "2534.332", "2600.668", "2602.918", "2609.666",
"2609.916", "2610.917", "2613.667", "2615.916", "2616.167", "2617.668",
"2619.917", "2621.167", "2621.417", "2627.917", "2631.167", "2659.221",
"2661.722", "2664.973", "2665.471", "2669.245", "2669.974", "2674.722",
"2676.972", "2682.973", "2684.721", "2685.722", "2734.047", "2734.546",
"2817.881", "2825.381", "2863.427", "2864.176", "2866.677", "2867.428",
"2868.426", "2868.926", "2876.427", "2880.177", "2880.178", "2896.924",
"2896.925", "2906.673", "2906.674", "2922.176", "2922.177", "2924.426",
"2924.427", "2926.676", "2928.178", "2929.177", "2932.428", "2933.928",
"2934.675", "2935.427", "2936.176", "2936.928", "2939.927", "2944.426",
"2944.427", "2950.174", "2951.677", "2952.427", "2953.677", "2954.426",
"2954.925", "2955.427", "2986.299", "2986.300", "2991.301", "2993.051",
"3000.302", "3000.801", "3001.302", "3002.552", "3004.300", "3009.798",
"3010.051", "3019.048", "3021.801", "3022.801", "3029.549", "3029.550",
"3044.304", "3044.305", "3048.550", "3048.551", "3070.050", "3070.051",
"3072.300", "3072.301", "3085.799", "3085.800", "3090.051", "3090.052",
"3119.050", "3119.051", "3125.799", "3125.800", "3128.550", "3128.551",
"3208.021", "3208.022", "3212.773", "3212.774", "3216.523", "3217.023",
"3229.774", "3232.522", "3235.773", "3235.774", "3238.774", "3240.274",
"3249.024", "3249.025", "3303.867", "3303.868", "3308.117", "3308.118",
"3316.866", "3319.619", "3326.117", "3351.366", "3357.920", "3359.948",
"3372.672", "3372.673", "3374.670", "3374.671", "3379.920", "3379.921",
"3384.672", "3388.423", "3399.423", "3400.171", "3411.975", "3412.472",
"3416.973", "3416.974", "3418.473", "3418.474", "3428.972", "3428.973",
"3437.222", "3437.223", "3458.724", "3458.725", "3460.473", "3460.474",
"3492.224", "3492.225", "3501.724", "3501.725", "3547.971", "3547.972",
"3587.152", "3590.651", "3590.652", "3601.651", "3604.653", "3606.402",
"3606.652", "3608.901", "3608.902", "3640.451", "3640.452", "3642.949",
"3642.950", "3649.203", "3649.703", "3652.453", "3653.203", "3664.953",
"3675.205", "3679.453", "369.672", "3696.954", "3712.704", "3717.703",
"372.672", "3732.453", "3732.955", "3740.202", "3740.203", "3746.203",
"3746.204", "3747.450", "3747.451", "3753.702", "3753.703", "3759.201",
"3839.400", "3844.910", "3847.911", "3850.907", "3851.657", "3857.908",
"3857.909", "3866.408", "3866.409", "3892.657", "3892.658", "3895.658",
"3895.659", "3909.909", "3909.910", "3919.408", "3919.409", "3928.656",
"3928.657", "3931.658", "3931.659", "3934.160", "3934.659", "3935.408",
"3935.409", "3945.909", "3945.910", "3947.660", "3950.909", "3955.159",
"3978.408", "3978.409", "4004.159", "4004.160", "4005.655", "4005.656",
"4010.657", "4010.658", "4013.408", "4013.409", "4049.113", "4062.624",
"4065.377", "4065.378", "4069.623", "4069.624", "4080.377", "4081.877",
"4093.627", "4100.628", "4110.378", "4110.379", "4129.875", "4131.627",
"4140.876", "4141.377", "4146.377", "4146.378", "4151.626", "4151.627",
"4156.374", "4156.375", "4259.823", "4259.824", "4265.323", "4265.324",
"4320.632", "4320.633", "4322.383", "4322.384", "4324.630", "4324.631",
"4325.634", "4327.881", "4335.882", "4338.884", "4351.133", "4374.936",
"4374.937", "4475.539", "4475.540", "4480.287", "4658.723", "4681.745",
"4709.773", "4721.773", "4773.588", "4785.849", "4874.925", "4875.927",
"4875.928", "4885.428", "4917.426", "4919.928", "4920.425", "4931.428",
"4932.928", "4935.927", "4937.429", "4972.781", "4985.531", "4988.282",
"4989.531", "4994.032", "4999.032", "5008.532", "5016.784", "5018.780",
"5018.781", "5093.404", "5093.405", "5096.403", "5096.404", "5099.155",
"5099.156", "5105.902", "5105.903", "5187.499", "5187.500", "5202.499",
"5202.500", "5225.782", "5225.783", "5238.553", "5238.554", "5267.326",
"5267.327", "5276.329", "5276.330", "5279.577", "5279.578", "5284.577",
"5286.080", "5300.080", "Cohort", "Condition", "Day", "Description",
"ID", "independent variables", "Media file(s)", "Observation date",
"Player #1", "Time", "Time offset (s)", "variable"), class = "factor"),
behavior = structure(c(7L, 7L, 7L, 5L, 7L, 5L, 7L, 7L, 7L,
7L, 4L, 4L, 7L, 7L, 4L, 4L, 4L, 4L, 7L, 7L, 4L, 4L, 7L, 7L,
4L, 4L, 7L, 7L, 5L, 5L, 7L, 7L, 4L, 4L, 7L, 7L, 5L, 5L, 7L,
7L, 5L, 5L, 7L, 7L, 4L, 4L, 4L, 4L, 7L, 7L), .Label = c("",
"Behavior", "drink", "eat", "groom", "on wheel", "walking"
), class = "factor"), status = structure(c(2L, 4L, 2L, 2L,
4L, 4L, 2L, 4L, 2L, 4L, 2L, 4L, 2L, 4L, 2L, 4L, 2L, 4L, 2L,
4L, 2L, 4L, 2L, 4L, 2L, 4L, 2L, 4L, 2L, 4L, 2L, 4L, 2L, 4L,
2L, 4L, 2L, 4L, 2L, 4L, 2L, 4L, 2L, 4L, 2L, 4L, 2L, 4L, 2L,
4L), .Label = c("", "START", "Status", "STOP"), class = "factor"),
real_beh = c("walking _ START", "walking _ STOP", "walking _ START",
"groom _ START", "walking _ STOP", "groom _ STOP", "walking _ START",
"walking _ STOP", "walking _ START", "walking _ STOP", "eat _ START",
"eat _ STOP", "walking _ START", "walking _ STOP", "eat _ START",
"eat _ STOP", "eat _ START", "eat _ STOP", "walking _ START",
"walking _ STOP", "eat _ START", "eat _ STOP", "walking _ START",
"walking _ STOP", "eat _ START", "eat _ STOP", "walking _ START",
"walking _ STOP", "groom _ START", "groom _ STOP", "walking _ START",
"walking _ STOP", "eat _ START", "eat _ STOP", "walking _ START",
"walking _ STOP", "groom _ START", "groom _ STOP", "walking _ START",
"walking _ STOP", "groom _ START", "groom _ STOP", "walking _ START",
"walking _ STOP", "eat _ START", "eat _ STOP", "eat _ START",
"eat _ STOP", "walking _ START", "walking _ STOP")), row.names = 20:69, class = "data.frame")

Here is what I get using dput with a header of 50:

dput(head(df, n=50))
structure(list(TIME = structure(c(205L, 209L, 2L, 3L, 3L, 4L,
5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L,
19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L,
32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L,
45L, 46L, 47L, 48L), .Label = c("", "2253.284", "2263.280", "2265.282",
"2266.031", "2269.281", "2272.780", "2276.281", "2277.031", "2294.033",
"2296.281", "2301.532", "2302.033", "2439.999", "2442.500", "2451.249",
"2451.750", "2459.248", "2459.249", "2462.000", "2464.498", "2464.999",
"2465.501", "2516.583", "2517.082", "2521.332", "2521.582", "2525.330",
"2525.832", "2533.082", "2534.332", "2600.668", "2602.918", "2609.666",
"2609.916", "2610.917", "2613.667", "2615.916", "2616.167", "2617.668",
"2619.917", "2621.167", "2621.417", "2627.917", "2631.167", "2659.221",
"2661.722", "2664.973", "2665.471", "2669.245", "2669.974", "2674.722",
"2676.972", "2682.973", "2684.721", "2685.722", "2734.047", "2734.546",
"2817.881", "2825.381", "2863.427", "2864.176", "2866.677", "2867.428",
"2868.426", "2868.926", "2876.427", "2880.177", "2880.178", "2896.924",
"2896.925", "2906.673", "2906.674", "2922.176", "2922.177", "2924.426",
"2924.427", "2926.676", "2928.178", "2929.177", "2932.428", "2933.928",
"2934.675", "2935.427", "2936.176", "2936.928", "2939.927", "2944.426",
"2944.427", "2950.174", "2951.677", "2952.427", "2953.677", "2954.426",
"2954.925", "2955.427", "2986.299", "2986.300", "2991.301", "2993.051",
"3000.302", "3000.801", "3001.302", "3002.552", "3004.300", "3009.798",
"3010.051", "3019.048", "3021.801", "3022.801", "3029.549", "3029.550",
"3044.304", "3044.305", "3048.550", "3048.551", "3070.050", "3070.051",
"3072.300", "3072.301", "3085.799", "3085.800", "3090.051", "3090.052",
"3119.050", "3119.051", "3125.799", "3125.800", "3128.550", "3128.551",
"3208.021", "3208.022", "3212.773", "3212.774", "3216.523", "3217.023",
"3229.774", "3232.522", "3235.773", "3235.774", "3238.774", "3240.274",
"3249.024", "3249.025", "3303.867", "3303.868", "3308.117", "3308.118",
"3316.866", "3319.619", "3326.117", "3351.366", "3357.920", "3359.948",
"3372.672", "3372.673", "3374.670", "3374.671", "3379.920", "3379.921",
"3384.672", "3388.423", "3399.423", "3400.171", "3411.975", "3412.472",
"3416.973", "3416.974", "3418.473", "3418.474", "3428.972", "3428.973",
"3437.222", "3437.223", "3458.724", "3458.725", "3460.473", "3460.474",
"3492.224", "3492.225", "3501.724", "3501.725", "3547.971", "3547.972",
"3587.152", "3590.651", "3590.652", "3601.651", "3604.653", "3606.402",
"3606.652", "3608.901", "3608.902", "3640.451", "3640.452", "3642.949",
"3642.950", "3649.203", "3649.703", "3652.453", "3653.203", "3664.953",
"3675.205", "3679.453", "369.672", "3696.954", "3712.704", "3717.703",
"372.672", "3732.453", "3732.955", "3740.202", "3740.203", "3746.203",
"3746.204", "3747.450", "3747.451", "3753.702", "3753.703", "3759.201",
"3839.400", "3844.910", "3847.911", "3850.907", "3851.657", "3857.908",
"3857.909", "3866.408", "3866.409", "3892.657", "3892.658", "3895.658",
"3895.659", "3909.909", "3909.910", "3919.408", "3919.409", "3928.656",
"3928.657", "3931.658", "3931.659", "3934.160", "3934.659", "3935.408",
"3935.409", "3945.909", "3945.910", "3947.660", "3950.909", "3955.159",
"3978.408", "3978.409", "4004.159", "4004.160", "4005.655", "4005.656",
"4010.657", "4010.658", "4013.408", "4013.409", "4049.113", "4062.624",
"4065.377", "4065.378", "4069.623", "4069.624", "4080.377", "4081.877",
"4093.627", "4100.628", "4110.378", "4110.379", "4129.875", "4131.627",
"4140.876", "4141.377", "4146.377", "4146.378", "4151.626", "4151.627",
"4156.374", "4156.375", "4259.823", "4259.824", "4265.323", "4265.324",
"4320.632", "4320.633", "4322.383", "4322.384", "4324.630", "4324.631",
"4325.634", "4327.881", "4335.882", "4338.884", "4351.133", "4374.936",
"4374.937", "4475.539", "4475.540", "4480.287", "4658.723", "4681.745",
"4709.773", "4721.773", "4773.588", "4785.849", "4874.925", "4875.927",
"4875.928", "4885.428", "4917.426", "4919.928", "4920.425", "4931.428",
"4932.928", "4935.927", "4937.429", "4972.781", "4985.531", "4988.282",
"4989.531", "4994.032", "4999.032", "5008.532", "5016.784", "5018.780",
"5018.781", "5093.404", "5093.405", "5096.403", "5096.404", "5099.155",
"5099.156", "5105.902", "5105.903", "5187.499", "5187.500", "5202.499",
"5202.500", "5225.782", "5225.783", "5238.553", "5238.554", "5267.326",
"5267.327", "5276.329", "5276.330", "5279.577", "5279.578", "5284.577",
"5286.080", "5300.080", "Cohort", "Condition", "Day", "Description",
"ID", "independent variables", "Media file(s)", "Observation date",
"Player #1", "Time", "Time offset (s)", "variable"), class = "factor"),
behavior = structure(c(7L, 7L, 7L, 5L, 7L, 5L, 7L, 7L, 7L,
7L, 4L, 4L, 7L, 7L, 4L, 4L, 4L, 4L, 7L, 7L, 4L, 4L, 7L, 7L,
4L, 4L, 7L, 7L, 5L, 5L, 7L, 7L, 4L, 4L, 7L, 7L, 5L, 5L, 7L,
7L, 5L, 5L, 7L, 7L, 4L, 4L, 4L, 4L, 7L, 7L), .Label = c("",
"Behavior", "drink", "eat", "groom", "on wheel", "walking"
), class = "factor"), status = structure(c(2L, 4L, 2L, 2L,
4L, 4L, 2L, 4L, 2L, 4L, 2L, 4L, 2L, 4L, 2L, 4L, 2L, 4L, 2L,
4L, 2L, 4L, 2L, 4L, 2L, 4L, 2L, 4L, 2L, 4L, 2L, 4L, 2L, 4L,
2L, 4L, 2L, 4L, 2L, 4L, 2L, 4L, 2L, 4L, 2L, 4L, 2L, 4L, 2L,
4L), .Label = c("", "START", "Status", "STOP"), class = "factor"),
real_beh = c("walking _ START", "walking _ STOP", "walking _ START",
"groom _ START", "walking _ STOP", "groom _ STOP", "walking _ START",
"walking _ STOP", "walking _ START", "walking _ STOP", "eat _ START",
"eat _ STOP", "walking _ START", "walking _ STOP", "eat _ START",
"eat _ STOP", "eat _ START", "eat _ STOP", "walking _ START",
"walking _ STOP", "eat _ START", "eat _ STOP", "walking _ START",
"walking _ STOP", "eat _ START", "eat _ STOP", "walking _ START",
"walking _ STOP", "groom _ START", "groom _ STOP", "walking _ START",
"walking _ STOP", "eat _ START", "eat _ STOP", "walking _ START",
"walking _ STOP", "groom _ START", "groom _ STOP", "walking _ START",
"walking _ STOP", "groom _ START", "groom _ STOP", "walking _ START",
"walking _ STOP", "eat _ START", "eat _ STOP", "eat _ START",
"eat _ STOP", "walking _ START", "walking _ STOP")), row.names = 20:69, class = "data.frame")

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) %>%
  summarise(counts=n())
---------------------
# A tibble: 2 x 2
next_event      counts
<chr>            <int>
 eat _ START          2
 walking _ START      6

Brilliant! I actually had a second question, which probably should have been asked first, however, I had to wait before posting another question: ** I was wondering how I could entirely remove walking behavior (walk_START and walk_STOP) since it is likely that walking will always occur after eating -- we are more interested in the other behaviors (grooming, drinking, etc.)

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.