how to write this if else statement in R? - r

Does anyone know how to write an if else statement in R where if the departure delay is more than 15 minutes then the airline has to pay $75 for every minute delayed and if the departure delay is less than 15 minutes then there is no charge?
This is what I wrote but its throwing an error
mutate(`Departure Delay charges`= if_else(`departure_delay`>= '16'|`DEP_DELAY`<='15',75*`departure_delay`, "0" ))

If you need > 15 mins, then the second dep_delay is not necessary. If true it will multiply departure_delay by 75, it it is FALSE - it will stay 0.
DepartureDelayCharges = ifelse(departure_delay >= 16, 75*departure_delay, 0)

Related

How do I convert milliseconds to dd:hh:mm using moment.js?

I'm trying to get the days, hours and minutes of 9000000 milliseconds, but moment.js is returning 0 days. I'm using Format plugin for the Moment Duration object. https://github.com/jsmreese/moment-duration-format
moment.duration(9000000, "milliseconds").format("dd:hh:mm");
returns "02:30"
How did I get 9000000?
var ms = moment.duration({
days: 1,
hours: 2,
minutes: 30,
})
console.log(ms._milliseconds);
// 9000000
Sounds like humanizeduration is what you are looking for:
humanizeDuration(97320000) // '1 day, 3 hours, 2 minutes'
Here is the github link:
https://github.com/EvanHahn/HumanizeDuration.js
1000 x 60 x 60 x 24 = 86'400'000 milliseconds.
Of course 9 mil is 0 days.
9'000'000 / (1000 x 60 x 60) = 2.5h = 2 hours 30 min
I hope I know how to use calculator
Check this place

How to restrict order open to a certain time and close it in another certain time with Interactive Brokers API

Using Interactive Brokers API, I would like to restrict order open to a certain time for example not before 09:35, I would also like to close the position at about 5 minutes before the end of the day.I tried to use an if statment with Sys.time() but I didn't work and in addition it is not elegant..How can I fix the error or use another method to full fill my need ?
Hour<-as.integer(format(Sys.time(), "%H"))
Minute<-as.integer(format(Sys.time(), "%M"))
print(lastValue)
library(IBrokers)
options("scipen"=4)
myconid = 3
twsobj = twsConnect(myconid)
Sys.sleep(2)
myorderid = as.integer(reqIds(twsobj))
print(myorderid)
Sys.sleep(2)
if(lastValue>0.5 && Hour > 16 && Minute > 35 ){
placeOrder(twsobj,Contract=twsSTK("SPY"),Order=twsOrder(myorderid ,"BUY",1,"MKT"))
print("IT WAS A BUY ORDER")
Sys.sleep(10)
placeOrder(twsobj,Contract=twsSTK("SPY"),Order=twsOrder(myorderid + 1 ,"SELL",1,"MKT"))
} else{
placeOrder(twsobj,Contract=twsSTK("SPY"),Order=twsOrder(myorderid , "SELL" , 1 , "MKT"))
print("IT WAS A SELL ORDER")
Sys.sleep(10)
placeOrder(twsobj,Contract=twsSTK("SPY"),Order=twsOrder(myorderid + 1 , "BUY" ,1, "MKT"))
}
Every broker has a facility to specify the good after time (GAT). It should be as simple as setting this field in the R interface. In the CRAN IBrokers docs it says the field
goodAfterTime Trades Good After Time: YYYYMMDD hh:mm:ss or ""
Here's the order properties API info at IB
https://www.interactivebrokers.com/en/software/api/apiguide/java/order.htm
The trade's "Good After Time," format
"YYYYMMDD hh:mm:ss (optional time zone)"
Note, MKT orders will be filled practically instantly. A LMT order may take some time.

VBScript echo every 30 seconds

The following code writes to the screen every iteration. Based on my understanding of the DateDiff documentation, it should only write every 30 seconds. What did I do wrong?
lasttime = Now
Do While Not data.eof
'looping through database records
if DateDiff(s,lasttime,Now) >= 30 Then
lasttime = Now
WScript.Echo "It's been 30 seconds..."
End if
Loop
Change this line:
if DateDiff(s,lasttime,Now) >= 30 Then
To this (note the quotes around "s")
if DateDiff("s",lasttime,Now) >= 30 Then

displaying current minute of a soccer match

I could not figure out the logic I would need to display a current minute of a soccer match. I have three fields in the database.
DateFirstStarted
DateSecondStarted
DateFullEnded
I should enter DateFirstStarted when the game starts and on the website. i.e. game starts at 7:05pm, on 7:25pm it should display '20 on the website. However, it should stop on the 45th minute. Then, I enter DateSecondStarted when the second half starts and should count from 46 to 90 and freeze there. Do I make sense? How can I do it? Is there a better way to do this?
Something not too complicated should do it. I will update start date of first half and start date of second half myself.
Here is how I tried it. I dont get an error but its not working. Any suggestion appreciated.
DateFirstStarted = objLiveCommentary("DateFirstStarted")
DateFirstEnded = DateAdd("n", 45, DateFirstStarted)
DateSecondStarted = objLiveCommentary("DateSecondStarted")
DateSecondEnded = DateAdd("n", 45, DateSecondStarted)
If DateFirstStarted => NOW() => DateFirstEnded Then
Response.Write "first half"
ElseIf DateSecondStarted => NOW() => DateSecondEnded Then
Response.Write "second half"
End If
Have a look at the DateDiff function?
You pass it two datetimes and it will give you the total amount of mins / seconds / hours etc between the two values.
http://www.w3schools.com/vbScript/func_datediff.asp
E.g.
If DateDiff("n",StartOfMatch,Now()) < 45 Then
FirstHalf = True
Elseif DateDiff("n",StartOfMatch,Now()) >= 45 Then
SecondHalf = True
Else
MatchEnded = True
End If
Hope this helps you?
C

Weird flex date issue

Flex is driving me CRAZY and I think it's some weird gotcha with how it handles leap years and none leap years. So here's my example. I have the below dateDiff method that finds the number of days or milliseconds between two dates. If I run the following three statements I get some weird issues.
dateDiff("date", new Date(2010, 0,1), new Date(2010, 0, 31));
dateDiff("date", new Date(2010, 1,1), new Date(2010, 1, 28));
dateDiff("date", new Date(2010, 2,1), new Date(2010, 2, 31));
dateDiff("date", new Date(2010, 3,1), new Date(2010, 3, 30));
If you were to look at the date comparisons above you would expect to get 30, 27, 30, 29 as the number of days between the dates. There weird part is that I get 29 when comparing March 1 to March 31. Why is that? Is it something to do with February only having 28 days? If anyone has ANY input on this that would be greatly appreciated.
public static function dateDiff( datePart:String, startDate:Date, endDate:Date ):Number
{
var _returnValue:Number = 0;
switch (datePart) {
case "milliseconds":
_returnValue = endDate.time - startDate.time;
break;
case "date":
// TODO: Need to figure out DST problem i.e. 23 hours at DST start, 25 at end.
// Math.floor causes rounding down error with DST start at dayOfYear
_returnValue = Math.floor(dateDiff("milliseconds", startDate, endDate)/(1000 * 60 * 60 * 24));
break;
}
return _returnValue;
}
This is not a leap year problem, but rather a daylight savings time problem.
To correct the code to account for DST, you need to look at the timezoneOffset of both dates to determine if the date range is spanning a DST boundary.
var adjustment:Number = ( startDate.timezoneOffset - endDate.timezoneOffset ) * 60 * 1000;
_returnValue = endDate.time - startDate.time + adjustment;
This will get the difference between the two time zones (in minutes), convert that value to milliseconds, and then apply the timezone difference to the millisecond difference to "cancel out" the DST boundary.
Naturally, when both numbers are in the same time zone, the adjustment value becomes 0 and the time values are not adjusted.
You have part of the answer in your comment: 2010-Mar-01 0:00 until 2010-Mar-31 0:00 is thirty (!) days minus one hour (because Mar 14 is DST start in 2010). Since you floor the result of your division, you get 29.
Edit: This answer is of course based on the assumption that the time property of Date takes DST into account. This would explain your problem; I didn't check it, however.

Resources