why does date() not work after loading lubridate package? - r

Every day, I use date() function in R to create a new working folder named with today's date. But, it does not work as soon as I load lubridate package. date() works again when I remove the package. What does lubridate do so 'date' function work differently? While loading the lubridate package, the message is:
Attaching package: ‘lubridate’. The following object is masked from
‘package:base’: date.
What should be the value of x in date(x) to get today's date and time?
date() #works well
library(lubridate)
date() #does not work now. Error Msg: Error in as.POSIXlt(x, tz = tz(x)) :
#argument "x" is missing, with no default
detach("package:lubridate", unload=TRUE)
date() #now it works again without "x"

Basically lubridate contains a function named "date" so when you load the lubridate package you are using date() function from that package and not from the base package.
If you want to use a specific function from lubridate package just type lubridate::"the name of the function goes here" without loading lubridate package.

The function date() gets masked out by the function that bears the same name lubridate::date()
A workaround is to use base::date()

Related

R Error in today() : could not find function "today"

I have an R script that just prints todays date. It runs just fine in Rstudio but when set as a task within a batch file it produces the following error
Warning message:
package 'dplyr' was built under R version 3.4.4
Loading required package: NLP
Warning message:
package 'tm' was built under R version 3.4.4
Error in today() : could not find function "today"
Execution halted
Here is the script:
library(rvest)
library(dplyr)
library(tm)
yesterday <- today()
yesterday <- gsub("-", "", yesterday, fixed=TRUE)
print(yesterday)
Batch File:
"C:\Program Files\R\R-3.4.2\bin\R.exe" CMD BATCH --vanilla --slave "C:\Users\mike\Desktop\Make_Task\TEST_YESTERDAY.R"
timeout /t 5
When you don't know where an R function comes from, I'd recommend searching rdocumentation.org for the name of the function. In these results, you can see that today is from the lubridate package.
Personally, I would recommend removing the external dependency by using the built-in Sys.Date() instead. But adding library(lubridate) to the top of your script should also work (assuming lubridate is installed).
The today() function is from the lubridate package. You probably loaded the package manually inside RStudio, so it's not in your code. Just add library(lubridate) to the beginning of your script and it should be fine.
Alternatively, you could also use Sys.Date() from r-base
Try with lubridate first and then try today() or now() you will get the answer.
install.packages("lubridate")
library("lubridate")
today()
[1] "2021-04-26"
now()
[1] "2021-04-26 07:09:37 UTC"

conflicting filter command in R

I am using the usual filter R command. However, when I run this on some data.frame, such basic as filter(data,data$entry==some_data), the output is a time serie. This is obviously related to the time series libraries I imported. How can I fix it ?
I imported the following libraries
library(ggplot2)
library(dplyr)
library(zoo)
library(stringi)
library(gridExtra)
library(rCharts)
library(xts)
library(tseries)
library(forecast)
library(curl)
library(vars)
library(astsa)
library(urca)
library(fGarch)
The default filter when you start R is stats::filter, it is used on time series. dplyr should mask it when loaded, so maybe you didn't load dplyr? Or maybe another package you loaded afterwards masked the dplyr version...
You can always specify the version you need by using package::function notation, e.g., dplyr::filter(data, ...). You can also check on conflicts (multiple definitions of objects) with conflicts().
As a side note, you should not be using $ inside dplyr::filter for the data you pass in, it is built to work with unquoted column names:
filter(data,data$entry==some_data) # bad
filter(data, entry == some_data) # good

Why is there no lubridate:::update function?

As said in the title: Why is there no such function? Or in a different way: What is the type of the function? When I type ?update I get something from stats package, but there is a lubridate function as described here on page 7. There also seems to be a lubridate:::update.Date function, but I can't find any explanations for that function.
Backround: I use the function in a package and I only got it to work after I used the Depends: in the decription file. Initially I wanted to use lubridate::update()...
The lubridate package provides the methods lubridate:::update.Date() and lubridate:::update.POSIXt(). Those functions are not exported into the namespace, but I assume that, by means of function overloading, they are invoked when update() is applied to a POSIXor Date object when the lubridate library is loaded.
The help page ?lubridate:::update.POSIXt provides some information concerning the update function within the lubridate package:
Description
update.Date and update.POSIXt return a date with the specified
elements updated. Elements not specified will be left unaltered.
update.Date and update.POSIXt do not add the specified values to the
existing date, they substitute them for the appropriate parts of the
existing date.
Usage
## S3 method for class 'POSIXt'
update(object, ..., simple = FALSE)
The usage section and the examples in the help page indicate that these functions don't need to be addressed individually, as they are called by simply using update() when the lubridate library is loaded.
To inspect these functions one can type, e.g., lubridate:::update.POSIXt in the console (without passing arguments, and without the parentheses).
You need to load the lubridate package:
library(lubridate)
date <- now()
print(date)
new_date <- update(date, year = 2010, month = 1, day = 1)
print(new_date)
Outputs:
"2016-08-04 08:58:08 CEST"
"2010-01-01 08:58:08 CET"

Lubridate Objects Masked After Loading Data.Table

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

lubridate error message 'incompatible method's when trying date arithmetic

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.

Resources