This questions is basically an extension to my following post R - Conditional operations on time variables and dates
The issue I have is due to an error occuring in the extension in form of a nested ifelse statement (e.g. see https://www.listendata.com/2017/03/if-else-in-r.html) accordingly the solution of @griffinevo. Assumed I have different closing times of the local stock exchanges (same time zone) I want to use nested ifelse statements to be able to distingush between the several closing times. The problem however, is the the following error:
Error in ifelse(StockExchanges %in% c("Vienna"), difftime(as.POSIXct(HMS, :
unused argument (ifelse(StockExchanges %in% c("Frankfurt"), difftime(as.POSIXct("22:00:00", format = "%H:%M:%S")) > 0, as.character(TimeStamp + 1), ifelse(StockExchanges %in% c("Frankfurt"), difftime(as.POSIXct("22:00:00", format = "%H:%M:%S")) > 0, as.character(TimeStamp + 1), as.character(TimeStamp))))
It seems that I have a mistake in the syntax, however I don't know what exactly is wrong.
Assumed, I have the following 3 Stock Exchanges: Vienna, Frankfurt and Hamburg and different closing times. I want to create a new variable TimeStampNew
and increase the day by one if the varibale HMS
is after the critical closing hours of the several stock exchange. Here, the first rows of the data frame df.
TimeStamp HMS StockExchange
1: 2013-09-15 19:54:00 Vienna
2: 2016-05-10 18:58:00 Vienna
3: 2014-09-22 22:47:00 Hamburg
4: 2012-01-30 18:03:00 Frankfurt
5: 2013-05-24 08:00:00 Vienna
6: 2013-06-07 11:45:00 Vienna
7: 2016-11-30 12:39:00 Vienna
8: 2007-03-28 NA Vienna
9: 2007-05-10 NA Hamburg
10: 2007-08-09 NA Vienna
11: 2007-11-08 NA Frankfurt
12: 2008-03-27 NA Vienna
This is my syntax producing the error above:
df$TimeStampNew <-
with(df,
ifelse(StockExchanges %in% c("Vienna") ,
difftime(as.POSIXct(HMS, format = "%H:%M:%S"),
as.POSIXct("17:35:00", format = "%H:%M:%S")) > 0,
as.character(TimeStamp + 1),
ifelse(StockExchanges %in% c("Frankfurt") ,
difftime(as.POSIXct("22:00:00", format = "%H:%M:%S")) > 0,
as.character(TimeStamp + 1),
ifelse(StockExchanges %in% c("Hamburg") ,
difftime(as.POSIXct("20:00:00", format = "%H:%M:%S")) > 0,
as.character(TimeStamp + 1),
as.character(TimeStamp)))))
df1$TimeStampNew[is.na(df1$HMS)] <- as.character(df1$TimeStamp[is.na(df1$HMS)])
Has anybody an idea where the mistake is to be found respectively has an alternative possible solution? Thanks in advance!
Aucun commentaire:
Enregistrer un commentaire