r - Delete grouped rows from a dataframe -
i have dataframe 48503 rows. dataframe has multiple user ids can make multiple simulations or 1 , way tell looking @ time difference between simulations user did. if difference in time on 20 secs long assume guy simulations done in different sessions.
i want make new column session number of simulation each user
(id) (simulation number) (simulation-time-difference) (session) 1 0:00:00.00 1 2 0:00:08.22 1 3 0:00:20.67 2 4 0:00:05.38 2 b 5 0:00:00.00 1 b 6 0:00:03.32 1 b 7 0:00:28.45 2
here 1 method using ave
, cumsum
after extracting seconds:
# extract seconds df$seconds <- as.numeric(gsub("^0:00:([0-9]{2}\\.[0-9]{2})", "\\1", df$v3)) # calculate session number df$session <- ave((df$seconds > 20), df$v1, fun=cumsum) + 1
this produces desired output.
df v1 v2 v3 seconds session 1 1 0:00:00.00 0.00 1 2 2 0:00:08.22 8.22 1 3 3 0:00:20.67 20.67 2 4 4 0:00:05.38 5.38 2 5 b 5 0:00:00.00 0.00 1 6 b 6 0:00:03.32 1 3.32 1 7 b 7 0:00:28.45 2 28.45 2
data
df <- read.table(text=" 1 0:00:00.00 2 0:00:08.22 3 0:00:20.67 4 0:00:05.38 b 5 0:00:00.00 b 6 0:00:03.32 b 7 0:00:28.45", as.is=true)
Comments
Post a Comment