Posts

Showing posts with the label dplyr

Calculating conditional cumulative time

Calculating conditional cumulative time Following the pointers from this question. I'd like to calculate the cumulative time for all the Cat s, by considering their respective last toggle status. Cat EDIT: I'd also want to check if the FIRST Toggle status of a Cat is Off and if it is so, for that specific cat , the time from midnight 00:00:00 till this first FIRST Off time should be added to its total conditional cumulative ontime. EDIT: Toggle Cat Off cat 00:00:00 Sample data: Time Cat Toggle 1 05:12:09 36 On 2 05:12:12 26R Off # First Toggle of this Cat happens to be Off, Condition met 3 05:12:15 26R On 4 05:12:16 26R Off 5 05:12:18 99 Off # Condition met 6 05:12:18 99 On 7 05:12:24 36 Off 8 05:12:26 36 On 9 05:12:29 80 Off # Condition met 10 05:12:30 99 Off 11 05:12:31 95 Off # Condition met 12 05:12:32 36 Off Desired sample output: Cat Time(Secs) 1 36 21 2 26R 18733 # (=1+18732), 18732 secs to be added = total Sec from midnight till 05:12:12...

for each row in a data frame, find whether there is a “close” row in another data frame

for each row in a data frame, find whether there is a “close” row in another data frame I have the following data frame: library(dplyr) set.seed(42) df <- data_frame(x = sample(seq(0, 1, 0.1), 5, replace = T), y = sample(seq(0, 1, 0.1), 5, replace = T), z= sample(seq(0, 1, 0.1), 5, replace = T) ) For each row in df , I would like to find out whether there is a row in df2 which is close to it ("neighbor") in all columns, where "close" means that it is not different by more than 0.1 in each column. df df2 So for instance, a proper neighbor to the row (1, 0.5, 0.5) would be (0.9, 0.6, 0.4) . The second data set is (1, 0.5, 0.5) (0.9, 0.6, 0.4) set.seed(42) df2 <- data_frame(x = sample(seq(0, 1, 0.1), 10, replace = T), y = sample(seq(0, 1, 0.1), 10, replace = T), z= sample(seq(0, 1, 0.1), 10, replace = T) ) In this case there is no "neighbor", so Im supposed to get "FALSE" for all rows of df . df My actual data frames are much bigger than th...