简体   繁体   English

我计算了一只老鼠在 1 分钟内做出反应的次数。 如何为老鼠没有响应的垃圾箱添加零?

[英]I counted the number of times that a rat responded within 1 min bins. How can I add a zero for bins that the rat did not respond in?

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.这是一个非常具体的问题,我的 eloquent 不足以找到更通用的说法,所以我提前道歉。 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.无论如何,我在 R 工作,拥有一组来自 15 只不同老鼠的数据。 The rats press a lever for 5-minute periods, and then the lever retracts for the next 25-minutes.老鼠按压杠杆 5 分钟,然后杠杆在接下来的 25 分钟内缩回。 This repeats for 6 hours, so there are 12 total 5-minute periods in which the rat can respond.这重复了 6 个小时,所以总共有 12 个 5 分钟的时间段可以让老鼠做出反应。 The rats had 11 total sessions of this.大鼠总共进行了 11 次这样的训练。

I recorded time stamps for each lever press over the course of each 6 hour session.在每 6 小时 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.因此,老鼠在 0 到 360 分钟的任何时间都会做出反应,除了每 25 分钟的时间段它们无法做出反应。 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 ).这就是我的问题所在。下面的代码是我用来计算每个 session ( session )的大鼠在每 5 分钟周期( component )内响应的次数。 However, to analyze the data properly, I need to also include components in which responses == 0 .但是,为了正确分析数据,我还需要包含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.我希望numReinforcers会包含零值。 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.完成创建垃圾箱后,我不需要时间戳,因此我可以灵活地将它们从 df 中取出以添加零响应的组件和分钟垃圾箱。 Again, hopefully that makes sense.再次,希望这是有道理的。 Thank you!谢谢!

Say you wanted to have all combinations between minute from 0 to 5 (6 elements), component from 1 to 12 (12 elements), and session (2 elements already present in df ).假设您希望在minute数从 0 到 5(6 个元素)、 component从 1 到 12(12 个元素)和session (2 个元素已经存在于df中)之间进行所有组合。

You could use:你可以使用:

res_full <- df %>% dplyr::select(-c(time, time2)) %>%
  unique %>% #removed duplicate rows
  complete(expand(df, minute = 0:5, session, component=1:12, rat), fill=list(numReinforcers=0)) # expanded the table, filling missing values with 0

dim(res_full)[1]==12*6*2 #144, TRUE

# A tibble: 144 x 5
   minute session component rat   numReinforcers
    <dbl>   <dbl>     <dbl> <chr>          <int>
 1      0       1         1 r1                 0
 2      0       1         2 r1                 0
 3      0       1         3 r1                 0
 4      0       1         4 r1                 0
 5      0       1         5 r1                 0
 6      0       1         6 r1                 0
 7      0       1         7 r1                 0
 8      0       1         8 r1                 0
 9      0       1         9 r1                 0
10      0       1        10 r1                 0
# ... with 134 more rows

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM