This is a really specific question, and I'm not eloquent enough to find a more general way to say this so I apologize in advance. My coding is really rusty and was never super good to begin with (I'm in neuroscience, not data). Anyway, I'm working in R and have a set of data from 15 different rats. The rats press a lever for 5-minute periods, and then the lever retracts for the next 25-minutes. This repeats for 6 hours, so there are 12 total 5-minute periods in which the rat can respond. The rats had 11 total sessions of this.
I recorded time stamps for each lever press over the course of each 6 hour session. So the rats respond anywhere from 0-360 minutes, with the exception of each 25-minute period in which they are unable to make a response. Sometimes, even when the lever is out, the rats do not make a response. This is where my question comes in. The code below is what I used to count the number of times the rat responded in each 5-min period (component) for each session (session). However, to analyze the data properly, I need to also include components in which responses == 0. My code doesn't allow for that. Is there a way I could tweak it, or add in another piece of code somewhere? Hopefully this makes sense; let me know if I can clarify anything. I'm adding sample data below. Thanks in advance!
# rat = id, using one rat as sample
# time = timestamp in minutes for each response over 6 hour session
# session = the session number, using session 1 and 2 as sample
df <- data.frame (rat = c("r1", "r1", "r1", "r1", "r1", "r1", "r1", "r1", "r1", "r1","r1", "r1", "r1", "r1", "r1", "r1", "r1", "r1", "r1", "r1"),
time = c(1.6883333,2.8716667,4.8316667,31.8066667,32.7000000,34.3166667,61.1783333,61.8316667,62.9183333,90.1800000, 1.7703928, 3.3195710, 150.7103710, 152.83091859, 271.6316667, 300.0500000, 300.2600000, 300.2947101, 330.0433333, 331.0938572),
session = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2))
# calculating the 5-min component in which the rat responded; does not include components with zero responses
df %>%
mutate(component = case_when(
time <= 5 ~ 1,
time < 35 | time <= 29.8 ~ 2,
time < 65 | time <= 59.8 ~ 3,
time < 95 | time <= 89.8 ~ 4,
time < 125 | time <= 119.8 ~ 5,
time < 155 | time <= 149.8 ~ 6,
time < 185 | time <= 179.8 ~ 7,
time < 215 | time <= 209.8 ~ 8,
time < 245 | time <= 239.8 ~ 9,
time < 275 | time <= 269.8 ~ 10,
time < 305 | time <= 299.8 ~ 11,
time < 335 | time <= 329.8 ~ 12
)) %>%
group_by(component) %>%
mutate(time2 = time - ((component-1)*30)) %>% # adding in 1-min bins to each component
ungroup() %>%
mutate(minute = case_when(
time2 <= 1 ~ 1,
time2 > 1 & time2 <= 2 ~ 2,
time2 > 2 & time2 <=3 ~ 3,
time2 >3 & time2 <=4 ~ 4,
time2 >4 ~ 5
)) %>%
group_by(rat, session, component, minute) %>%
mutate(numReinforcers = length(minute)) %>% # counting the number of responses per minute bin
ungroup()-> df
My hope is that numReinforcers would include zero values by the end of it all. I don't need the time stamps after I'm done creating the bins, so I'm flexible with taking them out of the df to add in components and minute bins with zero responses. Again, hopefully that makes sense. Thank you!