Write a function with date as input in R - r

I would like to write a function that will take date as input argument and output will be day, month, week and week year. My sample code shown some error. Kindly help me
in this regards, thank you.
My Sample code as follows:
myFunction <- function(date){
date <-as.numeric(as.Date(date, format = "%m/%d/%Y",origin = "1899-12-30"))
date$month<- strftime(date,"%m")
date$day<- strftime(date,"%d")
data$week<-strftime(date,"%w")
date$week_year<-strftime(date,"%W")
return(date$day,date$month,date$week,date$week_year)
}
When I called function ,It shown error:
myFunction(2016-07-26)
Error in as.POSIXlt.numeric(x, tz = tz) : 'origin' must be supplied

Your input is a string. Using lubridate you could write
myFunction <- function(date){
library(lubridate)
t0 <- ymd(date)
return(list(day(t0), month(t0), week(t0), wday(t0, label=F, abbr=F), year(t0)))
}

Related

Error in converting character to time variable in r with Lubridate packages

Thanks for your help.
One of variable in my dataset looks like this:
> df$TM
> [1] "000054" "000020" "000056" "000051" "000025" "000116" "000219" "000207" "000233" "000206" "000142" "000126" "000237" "000215" "000236" "000246" "000219"
[18] "000227" "000803" "000920"...
The real meaning of each character is hours, minutes and seconds.
When I adjust hms function in Lubridates as follows
> df$TM <- hms(df$TM)
Warning message is coming: "In .parse_hms(..., order = "HMS", quiet = quiet) :
Some strings failed to parse, or all strings are NAs"
After that, all the values in the column changes to NA.
I also tried
> df$TM <- as.POSIXct(df$TM, format = "%H:%M:%S")
and
> df$TM <- chronicle(times = df$TM)
and
> df$TM <- strptime(df$TM, format = "%H:%M:%S")
but... these three trial also have same results.
(Actually all data has changed to NA, so warning message is same as error message to me)
I really appreciate your help.
You can make use of this answer to include a semicolon after every second element. After that you can transform the resulting character string as date (with day, month and year) or leave it as is.
For completeness, the solution for your problem then is
as.POSIXct(sub(":+$", "", gsub('(.{2})', '\\1:', df$TM)), format = "%H:%M:%S")

Getting `Error in sort.list(bx[m$xi]) : 'x' must be atomic for 'sort.list'` error with `merge` in R

I want to join two tables on time. The input file has a column for hh:mm:ss and a column for am/pm. I seem to have successfully created a new column wih date information, but I can't join on it. The error message makes no sense to me and doesn't point to anything I gave it.
This:
t1 <- read.table("~/SEQ-1066/sar.r.54043.txt",header=TRUE)
t2 <- read.table("~/SEQ-1066/sar.q.54043.txt",header=TRUE)
t1$pt <- strptime(paste(t1$time, t1$AM),"%I:%M:%S %p")
t2$pt <- strptime(paste(t2$time, t2$AM),"%I:%M:%S %p")
t <- merge(x=t1,y=t2,by="pt")
results in:
> t1 <- read.table("~/SEQ-1066/sar.r.54043.txt",header=TRUE)
> t2 <- read.table("~/SEQ-1066/sar.q.54043.txt",header=TRUE)
> t1$pt <- strptime(paste(t1$time, t1$AM),"%I:%M:%S %p")
> t2$pt <- strptime(paste(t2$time, t2$AM),"%I:%M:%S %p")
> t <- merge(x=t1,y=t2,by="pt")
Error in sort.list(bx[m$xi]) : 'x' must be atomic for 'sort.list'
Have you called 'sort' on a list?
Thank you R for having such easy to understand error messages.
Apparently the datatype of strptime is not sortable or usable as a join index, so casting that using as.POSIXct worked.
t1 <- read.table("~/SEQ-1066/sar.r.54043.txt",header=TRUE)
t2 <- read.table("~/SEQ-1066/sar.q.54043.txt",header=TRUE)
t1$pt <- as.POSIXct(strptime(paste(t1$time, t1$AM),"%I:%M:%S %p"), tz = "GMT")
t2$pt <- as.POSIXct(strptime(paste(t2$time, t2$AM),"%I:%M:%S %p"), tz = "GMT")
t <- merge(x=t1,y=t2,by="pt")
Actual solution was found here, thank you Henrique Dallazuanna.

invalid 'tz' value, problems with time zone

I'm working with minute data of NASDAQ, it has the index "2015-07-13 12:05:00 EST". I adjusted the system time with Sys.setenv(TZ = 'EST').
I want to program a simple buy/hold/sell strategy, therefore I create a vector of flat positions as a foundation.
pos_flat <- xts(rep(0, nrow(NASDAQ)), index(NASDAQ))
Then I want to apply a constraint, that in a certain time window, positions are bound to be flat, which in my case means equal to 1.
pos_flat["T13:41/T14:00"] <- 1
And this returns the error:
"Error in as.POSIXlt.POSIXct(.POSIXct(.index(x)), tz = indexTZ(x)) :invalid 'tz' value".
I also get this error doing other calculations, I just used this example because it is easy and shows the problem.
As extra information:
> Sys.timezone
function (location = TRUE)
{
tz <- Sys.getenv("TZ", names = FALSE)
if (nzchar(tz))
return(tz)
if (location)
return(.Internal(tzone_name()))
z <- as.POSIXlt(Sys.time())
zz <- attr(z, "tzone")
if (length(zz) == 3L)
zz[2L + z$isdst]
else zz[1L]
}
<bytecode: 0x03648ff4>
<environment: namespace:base>
I don't understand the problem with the tz value... Any ideas?
The source of your "invalid 'tz' value" error is because, for whatever reason, R doesn't accept tz = df$var. If you set tz = 'America/New_York' or some other character value, then it will work.
Better answer (instead of using force_tz below) for converting UTC times to various timezones based on location. It is also simpler and better than looping through or using a nested ifelse. I subset and change tz based on a timezone column (which my data already has, if not you can create it). Just make sure you account for all timezones in your data
(unique(df$timezone))
df$datetime2[df$timezone == 'America/New_York'] <- format(df$datetime, tz="America/New_York")[df$timezone == 'America/New_York']
df$datetime2[df$timezone == 'America/Chicago'] <- format(df$datetime, tz="America/Chicago")[df$timezone == 'America/Chicago']
df$datetime2[df$timezone == 'America/Denver'] <- format(df$datetime, tz="America/Denver")[df$timezone == 'America/Denver']
df$datetime2[df$timezone == 'America/Los_Angeles'] <- format(df$datetime, tz="America/Los_Angeles")[df$timezone == 'America/Los_Angeles']
Previous solution: Converting to Local Time in R - Vector of Timezones
require(lubridate)
require(dplyr)
df = data.frame(timestring = c("2015-12-12 13:34:56", "2015-12-14 16:23:32"), localzone = c("America/Los_Angeles", "America/New_York"), stringsAsFactors = F)
df$moment = as.POSIXct(df$timestring, format="%Y-%m-%d %H:%M:%S", tz="UTC")
df = df %>% rowwise() %>% mutate(localtime = force_tz(moment, localzone))
df
You are getting errors because "EST" is not a valid timezone specification. It's an abbreviation that's often used when printing and displaying timezones.
The index is printed as "2015-07-13 12:05:00 EST" because "EST" probably represents Eastern Standard Time in the United States. If you want to set the TZ environment variable to that timezone, you should use Sys.setenv() with Country/City notation:
Sys.setenv(TZ = "America/New_York")
You can also set the timezone in the xts constructor:
pos_flat <- xts(rep(0, nrow(NASDAQ)), index(NASDAQ), tzone = "America/New_York")
Your error occurs because of a misinterpretation of the time object. You need to have UNIX timestamps in order to use something like
pos_flat["T13:41/T14:00"] <- 1
Try a conversion of your indices by doing something like this:
index(NASDAQ) <- as.POSIXct(strptime(index(NASDAQ), "%Y-%m-%d %H:%M:%S"))
As you want to use EST, you have to change your environment variables (if you are not living in EST timezone). So all in all, this should work:
Sys.setenv(TZ = 'EST')
#load stuff
#...
index(NASDAQ) <- as.POSIXct(strptime(index(NASDAQ), "%Y-%m-%d %H:%M:%S"))
pos_flat <- xts(rep(0, nrow(NASDAQ)), index(NASDAQ))
pos_flat["T13:41/T14:00"] <- 1
For further information, have a look at the POSIXct and POSIXlt structures in R.
Best regards

R bizdays trouble making it work

Im tring to use the bizdays package to generate a vector with bus days between two dates.
fer = as.data.frame(as.Date(fer[1:938]))
#Define default calendar
bizdays.options$set(default.calendar=fer)
dt1 = as.Date(Sys.Date())
dt2 = as.Date(Sys.Date()-(365*10)) #sample 10 year window
#Create date vector
datas = bizseq(dt2, dt1)
i get this error: "Error in bizseq.Date(dt2, dt1) : Given date out of range."
the same behavior for any function bizdays et al.
any ideas?
I had a similar problem, but could not apply the accepted answer to my case. What worked for me was to make sure that the first and last holiday in the vector holidays at least covers (or exceeds) the range of dates provided to bizdays():
library(bizdays)
This works (from_date and to_date both lie within the first and last holiday provided by holidays):
holidays <- c("2016-08-10", "2016-08-13")
from_date <- "2016-08-11"
to_date <- "2016-08-12"
cal <- Calendar(holidays, weekdays=c('sunday', 'saturday'))
bizdays(from_date, to_date, cal)
#1
This does not work (to_date lies outside of the last holiday of holidays):
holidays <- c("2016-08-10", "2016-08-11")
from_date <- "2016-08-11"
to_date <- "2016-08-12"
cal <- Calendar(holidays, weekdays=c('sunday', 'saturday'))
bizdays(from_date, to_date, cal)
# Error in bizdays.Date(from, to, cal) : Given date out of range.
If fer is the holidays, you can try with:
bizdays.options$set(default.calendar=Calendar(holidays=fer))

A function to load xts data into R

I'm trying to write a function that will load some xts data into R, and sort them by the date/time I specify. Maybe I'm trying a too simplistic method, but here is my code:
load.data <- function (x, time1, time2) #x is going to be the actual file name
{
vector = get(load("C:/Users/username/Desktop/x"))
sortvector = vector['time1/time2/']
return (sortvector)
}
And when I execute it, I get the message:
In readChar(con, 5L, useBytes = TRUE) :
cannot open compressed file 'C:/Users/username/Desktop/x', probable reason 'No such file or directory'
So, how can I make it so that my function will actually search for the file name, rather than actual generic 'x'? I hope I was clear, and I would certainly appreciate any help greatly.
Use paste or paste0
load.data <- function (x, time1, time2) #x is going to be the actual file name
{
vector = get(load(paste0("C:/Users/username/Desktop/", x)))
sortvector = vector[paste(time1, time2, sep='/')]
return(sortvector)
}
Also, look at ?FinancialInstrument:::saveSymbols.days and ?FinancialInstrument:::getSymbols.FI, or look at the code for those functions for examples of saving and loading xts objects
Edit
Here's an example
set.seed(123)
dat <- xts(cumsum(rnorm(100)), as.POSIXct("2012-05-23") + 1:100*60*60*6)
tmpdir <- tempdir()
save(dat, file=file.path(tmpdir, "dat.RData"))
rm('dat')
time1 <- "2012-05-24 10:00:00"
time2 <- "2012-05-25 11:00:00"
vector = get(load(paste(tmpdir, "dat.RData", sep="/")))
sortvector = vector[paste(time1, time2, sep='/')]
sortvector
# [,1]
#2012-05-24 12:00:00 -2.044404
#2012-05-24 18:00:00 -2.829308
#2012-05-25 00:00:00 -4.497250
#2012-05-25 06:00:00 -4.877477

Resources