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, 2019 in Data Analytics by Tyrion anex
• 8,700 points
913 views

1 answer to this question.

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, 2019 by Sophie may
• 10,620 points

Related Questions In Data Analytics

+1 vote
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,840 points
4,892 views
0 votes
2 answers

What is the difference between %% and % in R programming?

HI, %% returns remainder in case of numeric ...READ MORE

answered Aug 26, 2019 in Data Analytics by anonymous
• 33,030 points
2,436 views
0 votes
1 answer

Difference between [[1]] and [1] in R Programming

Hey Latha, The difference between them is that ...READ MORE

answered Jun 29, 2019 in Data Analytics by anonymous
• 33,030 points
6,649 views
+10 votes
3 answers

Which is a better initiative to learn data science: Python or R?

Well it truly depends on your requirement, If ...READ MORE

answered Aug 9, 2018 in Data Analytics by Abhi
• 3,720 points
1,664 views
+1 vote
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,840 points
4,873 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,840 points
838 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
• 10,620 points
724 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 28, 2018 in Data Analytics by Sophie may
• 10,620 points
1,369 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP