Changing String to Date and Time in R - r

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.

Related

How to solve the following error message related to dates :Incompatible methods ("-.Date", "-.POSIXt") R?

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!

R - Error in charToDate(x)

I'm sure this is super simple to understand, but I'm new to R, and try as I might, I can't get the other suggestions on these forums to work for me. I'm just trying to download the PAYEMS time series through the Quantmod package.
The code:
library(quantmod)
library(lubridate)
getSymbols("PAYEMS", src=("FRED"), return.class = "xts")
The output:
getSymbols("PAYEMS", src=("FRED"), return.class = "xts")
Error in charToDate(x) :
character string is not in a standard unambiguous format
I'm guessing it's the date format, but it's not clear to me how I fix that. I only know how to change it once I get the vector I want to make into dates.
Thanks!
I get this error occasionally too. I don't recall getting this the year before. Restarting R doesn't help, but restarting the computer does solve the problem. I don't know what it is that fixed this.

Troubles in R with Dates

I'm just starting with R. I have the following problem:
I am using the "sesonal" package (http://cran.at.r-project.org/web/packages/season/season.pdf). I wanted to usethe cosinor function but I have a problem I can't solve.
If I use this call:
test = cosinor(data=train_data2, date=as.Date("2013-01-01"))
I get:
Fehler in charToDate(x) :
character string is not in a standard unambiguous format
but the date seams to be in the right format since
as.Date("2013-01-01")
works fine.
can anyone help me ?
Thanks in advance!

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.

getSymbols (quantmod) giving wrong dates

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.

Resources