I'm trying to learn lubridate. The lubridate documentation shows:
date <- ceiling_date(date, "month") - days(1)
[1] "2010-05-31 UTC
for date arithmetic. But if I try
mytoday <- now()
first_of_month <- floor_date(mytoday, "month")
first_of_month_last_year <- first_of_month - years(1)
to use date arithmetic to get the first of the month a year earlier I get an error message
Warning message:
Incompatible methods ("-.POSIXt", "Ops.ordered") for "-"
Any ideas what I'm doing wrong? Thanks.
Ran into the same problem.
In my case it was that I had two date/time related packages installed (chron and lubridate), which appears to conflict with each other to some degree.
I solved this by removing chron as I wasn't really using it.
If you are using multiple date/time related packages, I would suggest removing these packages, then reinstalling them, with lubridate last. However, you may run into other conflicts.
Hope this helps.
Related
I am new using R, I need to calculate the time between two dates: date_inclusion and date_point, for which I am doing the following.
cancer$date_point <- as_date("1980 - 02 - 15")
cancer$delai_recul_j <-(cancer$date_point-cancer$Date_inclusion)
but I get the following message.
'Warning message: Incompatible methods ("-.Date", "-.POSIXt") for "-"'
At first I thought it was the Mac date format, but it didn't work, I don't know what to do, since obviously with the column that is created I can't work.
Thank you very much!
When I load the data.table package after having already loaded the lubridate package, I get the following error message:
Loading required package: data.table
data.table 1.9.4 For help type: ?data.table
*** NB: by=.EACHI is now explicit. See README to restore previous behaviour.
Attaching package: ‘data.table’
The following objects are masked from ‘package:lubridate’:
hour, mday, month, quarter, wday, week, yday, year
Does anyone know a) what's causing this issue and b) how to prevent these objects within lubridate from being masked?
UPDATE:
The issue associated with the above is that I'm using the quarter function from the lubridate package and, after loading the data.table package, I can no longer do so in the same way.
Specifically, when I run quarter(Date, with_year=TRUE) (where Date is a vector of class = Dates), I now get the following error: Error in quarter(Date, with_year = TRUE) : unused argument (with_year = TRUE).
If I simply, quarter(Date), then I can get the desired output without the attached year. For example, if Date is set as simply May 15, 2015 (today), then quarter(Date) will yield 2 (since we're in the 2nd quarter of 2015), but I'd like it to yield 2015.2, hence the importance of the with_year = TRUE option.
Obviously, I can overcome this by using paste to bind together the year and the output of quarter(Date), but I'd prefer to avoid that work-around.
An object name in a package namespace is masked when a new object is defined with the same name. This can be done by the user assigning the name, or by attaching another package that has an object of the same name.
data.table and lubridate have overlapping function names. If you want the lubridate version to be the default, then the easiest solution is to load data.table first, then load lubridate---thus it will be the data.table versions of these functions that is masked by the "newer" lubridate versions.
library(data.table)
library(lubridate)
Otherwise, the solution is to use :: (as in package::function) to fully specify which version of the function you want to use, for example:
lubridate::quarter(Date, with_year = T)
Another option, which involves a little less typing but is perhaps a little less clear as well, would be to alias the lubridate functions you want in the global environment at the start of your script.
quarter = lubridate::quarter
Any use of quarter() later in the script will use the lubridate version of the function.
Yet another option is the conflicted package, which provides a system for preferring a function from one package. It is a bit more intense and intentional, you should definitely read the documentation before using it, but your script might include something like this:
library(conflicted)
conflict_prefer("quarter", "lubridate")
The package conflicted provides various alternatives and is a good practice to use it while loading libraries to be clear on the masking.
https://github.com/r-lib/conflicted
I have a vector of character class strings.
I want to covert this to Sys.time() format. How can I do that?
When I use the as.POSIXct(z,format= "%d/%m/%Y %H:%M:%S")
I get the following warning:
Warning message:
In as.POSIXlt.POSIXct(x, tz) : NAs introduced by coercion
I don't want this to convert to NAs. What should I do?
Kindly use
strptime("2014-06-05 15:33:55 IST","%Y-%m-%d %H:%M:%S",tz="Asia/Kolkata")
For your problem
data$time <- strptime(data$time, "%Y-%m-%d %H:%M:%S",tz="Asia/Kolkata")
class(data$time)
It should be of type POSIXlt
I know this is an old question but for the benefit of those Googling this problem, I had a similar problem recently when using rbind.fill. I discovered that one of my dates was in European format, 31/1/2017 instead of 1/31/2017 like all the others. It was causing R to blank out the entire column after performing rbind.fill. Fixing the date fixed the issue.
Hope this helps someone, I spent hours trying to figure out why this was happening. R is really picky sometimes.
I'm using the quantmod package to fetch stock data. The code
Data = getSymbols('LON:ADN',src="google",auto.assign=FALSE, from = '2011-08-10')
Results in an xts as expected, however on closer examination it shows a volume of trades for 2012-10-21 (October 21st) which was a sunday, and is therefore clearly erroneous. Several other sundays are also included. Unfortunately the errors surrounding the weekends seem to have moved the rest of the data out of alignment.
Has anyone experienced similar problems fetching tickers with quantmod before, and if so, are they aware of a way around them?
Thanks
As you mentioned in the comments, this looks like a timezone issue, possibly due to POSIX date conversion in the xts function (see this answer).
I am able to reproduce the issue in a fresh R session when Sys.getenv("TZ") is an empty character string. Setting the timezone to any valid timezone (not all tested), for example, "America/Chicago" yields expected dates, i.e., no Sundays:
In a fresh session (December 16th 2012 was a Sunday):
Sys.getenv("TZ")
# [1] ""
library(quantmod)
Data <- getSymbols('LON:ADN',src="google",auto.assign=FALSE, from = '2011-08-10')
tail(index(Data))
# [1] "2012-12-13" "2012-12-16" "2012-12-17" "2012-12-18" "2012-12-19" "2012-12-20"
Then change the timezone
Sys.setenv(TZ="America/Chicago")
Data <- getSymbols('LON:ADN',src="google",auto.assign=FALSE, from = '2011-08-10')
tail(index(Data))
# [1] "2012-12-14" "2012-12-17" "2012-12-18" "2012-12-19" "2012-12-20" "2012-12-21"
No Sundays.
The function Sys.Date() returns a date object, but on different machines running R2.12 I get different outputs
> Sys.Date()
[1] "08/17/2011 00:00:00.000 UTC"
vs.
> Sys.Date()
[1] "2011-08-17"
The former looks like a POSIX object, even though it's still a date object. What possible differences in settings between the two environments may cause this problem?
Could it be that the systems are set to different locales? Try
Sys.getlocale();
Another possible scenario is that some nefarious soul "overwrote" the Sys.Date() function with another. Try running
base::Sys.Date();
on both systems. If you have a package, other than base, that is loading a function called Sys.Date() then you should call base::Sys.Date() to be sure you get the right one.