r - time difference between two same tweet -
how calculate time difference between 2 same tweet(from text perspective) , add column in data frame show time difference timediff(new column name ) td3 = t3-t2. td13 = t13-t12 if t13 , t12 both same text/tweet.
input data
tweet text time-stamp
rt aamaadmiparty arvindkejriwal leaves 2014-03-24 17:18:53
rt aamaadmiparty arvindkejriwal leaves 2014-03-24 22:37:11 rt aamaadmiparty arvindkejriwal leaves 2014-03-24 22:44:51
rt aamaadmiparty arvindkejriwal leaves 2014-03-25 13:24:31
rt aamaadmiparty download aap 2014-03-25 19:31:06 rt aamaadmiparty download aap 2014-03-25 19:34:29 rt aamaadmiparty download aap 2014-03-25 19:42:10 rt aamaadmiparty download aap 2014-03-25 19:53:38
desired output data
tweet text time-stamp t-diff
rt aamaadmiparty arvindkejriwal leaves 2014-03-24 17:18:53 0 rt aamaadmiparty arvindkejriwal leaves 2014-03-24 22:37:11 5.305 hours rt aamaadmiparty arvindkejriwal leaves 2014-03-24 22:44:51 5.432778 hours
rt aamaadmiparty download aap 2014-03-25 19:31:06 0 rt aamaadmiparty download aap 2014-03-25 19:34:29 3.383333 mins rt aamaadmiparty download aap 2014-03-25 19:42:10 11.06667 mins rt aamaadmiparty download aap 2014-03-25 23:36:57 4.0975 hours
this code working how add in column using loop large dataset not clear me .... df working dataframe ..
difftime(df[49,2],df[48,2]) time difference of 3.383333 mins difftime(df[51,2],df[48,2]) time difference of 11.06667 mins df$diff_time<-(difftime(df[65,2],df[48,2])) difftime(df[65,2],df[48,2]) time difference of 4.0975 hours
an illustration
# toy data time_1 = strptime("3/22/2014 15:08", "%m/%d/%y %h:%m") time_2 = strptime("3/22/2014 15:15", "%m/%d/%y %h:%m") id = "user_1" df = data.frame(id, time_1, time_2) # id time_1 time_2 # 1 user_1 2014-03-22 15:08:00 2014-03-22 15:15:00 # base r df$diff_time <- with(df, time_2 - time_1) # dplyr library(dplyr) df %>% mutate(diff_time = time_2 - time_1) # # id time_1 time_2 diff_time # 1 user_1 2014-03-22 15:08:00 2014-03-22 15:15:00 7 mins
Comments
Post a Comment