0
votes

I have measured a response ('y') over time ('x') in a group of animals ('subject') in a set of conditions ('factor1','factor2'). The response was measured continuously for a fixed period of 20 min after a stimulus of duration = 'z' was given.

For these data, I would like to compute the time taken (here denoted 'duration') for 'y' to return to its baseline value (which is 0) after the stimulus ended, grouping the data by 'subject', 'factor1' and 'factor2'. Here is an example data set

data<-
    data.frame(x=rep(rep(1:20,4),6),y=rnorm(480,mean=4,sd=2),z=rep(3,80),
    factor1=rep(rep(c("A","B"),each=20),4),
    factor2=rep(c(rep("C",20),rep("D",20),rep("C",20),rep("D",20))),
    subject=rep(factor(1:6),each=80))

I tried to solve this using dplyr:

library("dplyr")
data %>%
    group_by(subject,factor1,factor2) %>%
    mutate(duration=nth(x,first(which(y<=0)))-z)

This yields the error "Error in mutate_impl(.data, dots) : Evaluation error: missing value where TRUE/FALSE needed."

I thought that this might occur as some subjects never returned to baseline, so I tried amending the code by setting those observations to 'duration'=20:

data %>%
group_by(subject,factor1,factor2) %>%
mutate(duration=ifelse(
(nth(x,first(which(y<=0)))-z)<=(20-z),
(nth(x,first(which(y<=0)))-z),20)
)

However, the error message remains "Error in mutate_impl(.data, dots) : Evaluation error: missing value where TRUE/FALSE needed."

In both cases, the error message disappears when I remove the "group_by" statement, but I cannot quite figure out why (apart from the fact that some individuals never returned to baseline).

How do I best go about solving this? I assume I might be missing something quite obvious...

Many thanks, Andreas

1
Please show a small reproducible example instead of a data in a link to downloadakrun
Yes, of course. Sorry. Post has been amended.Andreas Nord
The problem is that some of your groups have no negative 'y' and then which(y<=0) evaluates to NAtjebo

1 Answers

0
votes

See my comment. Your which() call evaluates to NA in some groups. So you need to specify how to deal with those cases. Eg, replace with NA:

data %>%
  group_by(subject,factor1,factor2) %>%
  mutate(duration= ifelse(is.na(first(which(y<=0))),NA, nth(x,first(which(y<=0)))-z))

Also, I would recommend against the use of factors, they are messing up a lot if you don't understand what they actually are (I don't, so I don't use them). You can use characters instead.