-2

I have a DF which looks like this (but actually with a lot more data!):

https://i.stack.imgur.com/71RBH.jpg

I am trying to look at every Subject and determine what the "Conditions" were before the "Trigger" switched to 1,2 or 3. Specifically I am trying to create a new DF with information containing three "Condtitions" before the "Trigger" turns to 1,2 or 3.

if(df$Trigger >= 1){
  copie 3 rows before trigger (including row with trigger>=1) >= 1 of same Subject and delete rest.
}

The Result should look like this:

https://i.stack.imgur.com/60w4d.jpg

I have been trying to figure this out for weeks but i am too inexperienced. I am grateful for any help.

pushkin
  • 9,575
  • 15
  • 51
  • 95
  • 1
    Welcome to Stack Overflow! Please provide a [reproducible example in r](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). The link I provided, will tell you how. Please take/review the [tour](https://stackoverflow.com/tour) and read up on [How to Ask](https://stackoverflow.com/help/how-to-ask), and edit the question accordingly. If you are asking us to debug, please include what's called a [Minimal, Complete, and Verifiable Example](https://stackoverflow.com/help/mcve); ensure you have your code, sample inputs, outputs, and, if any, errors included. – M-- Apr 30 '19 at 18:47

1 Answers1

0

From what you were saying above with the if statement is what I took this off of. Basically it is finding where the switches happen, finds the three rows before switch and includes it in the FinalDF:

library(tidyverse)
df <- tibble::tribble(
    ~Subject, ~Testperiod, ~Condition, ~Trigger,
           1,           1,          0,        0,
           1,           2,          0,        0,
           1,           3,          1,        0,
           1,           4,          2,        0,
           1,           5,          3,        0,
           1,           6,          3,        1,
           1,           7,          1,        1,
           1,           8,          1,        1,
           1,           9,          0,        1,
           1,          10,          0,        1,
           1,          11,          0,        1,
           1,          12,          0,        0,
           2,           1,          0,        0,
           2,           2,          2,        0,
           2,           3,          3,        0,
           2,           4,          3,        0,
           2,           5,          3,        2,
           2,           6,          2,        2,
           2,           7,          1,        1,
           2,           8,          2,        1,
           2,           9,          0,        1,
           2,          10,          0,        0,
           2,          11,          0,        0,
           2,          12,          0,        0
    )

colchanges <- which(df$Trigger != dplyr::lag(df$Trigger))
ChangesDF <- cbind(rownum = colchanges,value = df[colchanges,"Trigger"])
rows <- dplyr::filter(ChangesDF,Trigger %in% c(1:3)) %>% select(rownum) %>% 
    mutate(One = rownum - 1,
           Two = rownum - 2,
           Three = rownum - 3)
rows <- sort(as.vector(t(rows)))
rows <- rows[rows > 0]

FinalDF <- df[rows,]
FinalDF
# A tibble: 12 x 4
   Subject Testperiod Condition Trigger
     <dbl>      <dbl>     <dbl>   <dbl>
 1       1          3         1       0
 2       1          4         2       0
 3       1          5         3       0
 4       1          6         3       1
 5       2          2         2       0
 6       2          3         3       0
 7       2          4         3       0
 8       2          4         3       0
 9       2          5         3       2
10       2          5         3       2
11       2          6         2       2
12       2          7         1       1
Big_Ozzy
  • 275
  • 4
  • 13
  • I currently get the following error: Error in rows[rows, ] : incorrect number of dimensions. Could this be because sometimes the Trigger happens in Testperiod 1? In this case we can't subtract 1 row but would leave it as it is. Or if the Trigger happens in Testperiod 2 then we would have to only subtract 1 row? Would a group_by(Subject) be helpfull in this case? Thanks for your help so far i would have never come up with this =) – rofl4breakfast Apr 30 '19 at 20:07