Loop through dataframe in R and measure time difference between two values

前端 未结 2 1680
囚心锁ツ
囚心锁ツ 2021-01-27 12:12

Summary: I am analyzing the time difference between an occured stimuli (A&B) and a possible response of the user.

The dataset has the following stru

相关标签:
2条回答
  • 2021-01-27 12:48

    Here's how you can do that with dplyr. First, you need to transform your Date column to a POSIXct object. Then, make sure the Date object is ordered with arrange. You then add a time difference column using mutate. You can then filter for rows where Stimuli A or B is 1 and is followed by a Response equal to 1.

    df$Date <- as.POSIXct(strptime(df$Date,"%d.%m.%Y %H:%M"))
    df %>%
      arrange(User,Date)%>%
      mutate(difftime= difftime(lead(Date),Date, units = "mins") ) %>%
      group_by(User)%>%
      filter((StimuliA==1 | StimuliB==1) & lead(Responses)==1)
    
                                      User                Date  Hour StimuliA StimuliB Responses   difftime
                                     <chr>              <dttm> <int>    <int>    <int>     <int>     <time>
    1 005b98f3-5b1b-4d10-bdea-a55d012b2844 2015-11-25 13:59:00  1645        1        0         0 11253 mins
    2 005b98f3-5b1b-4d10-bdea-a55d012b2844 2015-12-07 08:18:00  1928        1        0         0  2122 mins
    3 005b98f3-5b1b-4d10-bdea-a55d012b2844 2015-12-08 19:40:00  1963        0        1         0 19510 mins
    4 005b98f3-5b1b-4d10-bdea-a55d012b2844 2016-01-05 11:35:00  2627        0        1         0   106 mins
    5 005b98f3-5b1b-4d10-bdea-a55d012b2844 2016-01-06 09:18:00  2649        1        0         0 56969 mins
    
    0 讨论(0)
  • 2021-01-27 12:54

    SQL syntax should be able to get you your answer and is the conventional method for querying tabular data like these. The Data.Table package makes this sort of syntax accessible.

    #import necessary library
    library(data.table)
    
    #instantiate data table
    dt<-data.table(dt)
    
    #convert date field to Date type
    dt$Date <- as.POSIXct(dt$Date, format="%d.%m.%Y %H:%M")
    #create another date field so as not to lose during join
    dt$rollDate<-dt$Date
    
    #create table with stimuliA and set key for sorting/joining purposes
    stima.dt <- dt[StimuliA==1,.(User,rollDate,Date,Hour,StimuliA)]
    setkey(stima.dt,User,rollDate)
    
    #Same for stimuliB
    stimb.dt <- dt[StimuliB==1,.(User,rollDate,Date,Hour,StimuliB)]
    setkey(stimb.dt,User,rollDate)
    
    #same for responses table
    resp.dt <- dt[Responses==1,.(User,rollDate,Date,Hour,Responses)]
    setkey(resp.dt,User,rollDate)
    
    #Join stimuli A table to closes responses
    stim.a<-resp.dt[stima.dt,roll=-Inf]
    
    #calculate Hour differences
    stim.a[,difftime(Date,i.Date,units="min")]
    
    #Join stimuli B table to closes responses
    stim.b<-resp.dt[stimb.dt,roll=-Inf]
    
    #calculate Hour differences
    stim.b[,difftime(Date,i.Date,units="min")]
    
    0 讨论(0)
提交回复
热议问题