Search code examples
rfor-looplag

R - for loop - using values in previous row


create a new column in R, using timepoints corresponding to a rating in another column.


Solution

  • Tidyverse solution:

    install.packages("dplyr", dependencies = TRUE)
    library(dplyr)
    dataframe.df %>% 
      as.data.frame() %>% 
      mutate(Timepoint = as.numeric(gsub("[a-zA-Z]+", "", Timepoint))) %>% 
      group_by(Replicate, Stimulus, Subject) %>% 
      mutate(start_end_time = ifelse(Timepoint != max(Timepoint), min(Timepoint), max(Timepoint))) %>%
      ungroup()