R programming: loop difference

0 votes

I have two variables and I would like to loop them such that, it should calculate arrival of(i) - departure of (i+1) and it should store the result in a new column

departure                            arrival
2012-01-26 08:15:00                 2012-01-26 08:50:00
2012-01-26 11:30:00              2012-01-27 16:00:00
2012-01-27 18:00:00               2012-01-27 23:05:00
2012-02-11 00:20:00                 2012-02-11 09:10:00

expected output
waiting time
(2012-01-26 08:50:00)-(2012-01-26 11:30:00) =  2 hr 40 min
 (2012-01-27 16:00:00)-( 2012-01-27 18:00:00) =  2 hrs
 (2012-01-27 23:05:00)- (2012-02-11 00:20:00)=14 days 13 hrs, 349 hrs

I tried this, but I kept getting errors:

for (i :length(data frame){
i <- i[17] #index of arrival column
j <- i+1[18] #index of departure column
diff = i-j
return diff
}
diff = waiting time
Jan 17 in Data Analytics by Tyrion anex
• 8,280 points
32 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

Use the lubridate package to do this:

d$departure <- lubridate::ymd_hms(d$departure)
d$arrival   <- lubridate::ymd_hms(d$arrival)

d$difference <- d$departure - d$arrival
d
answered Jan 17 by Sophie may
• 9,050 points

Related Questions In Data Analytics

0 votes
1 answer

Difference between factor and as.factor in R programming

Hey @Ali, as.factor is a wrapper for ...READ MORE

answered Oct 29, 2018 in Data Analytics by Maverick
• 10,000 points
18 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Error saying "vector size cannot be NA" when using R with data mining

You can use the removesparseterm function.  Removes sparse ...READ MORE

answered Nov 15, 2018 in Data Analytics by Maverick
• 10,000 points
186 views
+1 vote
2 answers
0 votes
1 answer

Trying to find frequent itemsets of a data set using arules package

Try replacing ID <- c("A123","A123","A123","A123","B456","B456","B456") item <- c("bread", "butter", "milk", ...READ MORE

answered Nov 15, 2018 in Data Analytics by Maverick
• 10,000 points
16 views
0 votes
1 answer

Error saying "Error in df$item : object of type 'closure' is not subsettable" when trying to use arules package

Try replacing ID <- c("A123","A123","A123","A123","B456","B456","B456") item <- c("bread", "butter", ...READ MORE

answered Nov 15, 2018 in Data Analytics by Maverick
• 10,000 points
51 views
0 votes
1 answer

Can we have an if loop inside a for loop in R programming?

You're If loop doesn't have any condition ...READ MORE

answered Dec 21, 2018 in Data Analytics by Sophie may
• 9,050 points
34 views
0 votes
1 answer

R programming: Finding the difference between 2 vectors

Try this function, it worked for me: f ...READ MORE

answered Dec 27, 2018 in Data Analytics by Sophie may
• 9,050 points
30 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.