Character to Date - r

I am having some problems with changing dates from Excel (characters in R) into date in R.
Knowing that I am using : as.Date(Data$Datum, "d%/m%/y%")
But it returns NA NA NA..
And the date starts in Excel on : 1-1-2019 , but in R it shows : "43466"
I have tried also lubridate : dmy(Data$Datum) , but it also returns all the values as NA.
Could you please help me with this situation!
Thanks in advance!

Fix this part and everything will be ok,
date <- as.Date(Data$Datum, "%m-%d-%Y")
Place the % sign in front and use Y not y because it is 2019 and not 19.
Finally, separate mdY with "-"

Related

as.Date giving me NA's

I've tried everything in this thread as.Date returning NA while converting from 'ddmmmyyyy' to try and sort my problem.
I'm using these commands to turn a factor into a date:
cohort$doi <- as.Date(cohort$doi, format= "%Y/%m/%d")
All my dates are currently in the format: YYYY-MM-DD, so as far as I'm aware the above should work
I used this code yesterday to convert all my dates for various variables from a factor to a date. It worked yesterday and everything was fine. Today I opened my script and imported in my data, ran this command and viewed my data but all of the dates now say NA.
I've tried everything from previous threads (I looked at a few more than just the one I linked above) but nothing has so far worked. I'm not sure what to do now
Example of what doi column looks like:
1970-01-01
1970-02-02
1970-03-03
1970-04-04
The column is currently classed as an factor. And when I do the code I used above, the column is defined as a date but all the dates now say NA
Other than closing R and opening it up again for today, I've done nothing else.
If you read the documentation for as.Date you will note the default format is %Y-%d-%m or %Y/%d/%m:
The default formats follow the rules of the ISO 8601 international standard which expresses a day as "2001-02-03".
In your code you have specified your dates are formatted by slashes, but your sample data shows they are formatted in the default format used by as.Date:
doi <- as.factor(c("1970-01-01",
"1970-02-02",
"1970-03-03",
"1970-04-04"))
as.Date(doi) # default format %Y-%m-%d
[1] "1970-01-01" "1970-02-02" "1970-03-03" "1970-04-04"
as.Date(doi, format = "%Y/%m/%d") # incorrect specification of your date format
[1] NA NA NA NA
as.Date("1970/01/01") # also a default format
[1] "1970-01-01"
Note: as.Date accepts character strings, factors, logical NA and objects of classes "POSIXlt" and "POSIXct".

R: as.POSIXct returns NA

I am new on manipulating time in R. I am trying to turn a character a="201304021500" into POSIXct form using the following code:
#First I turn my charcter a into the format "2013/04/02 15:00"
annee=substr(a,1,4)
mois=substr(a,5,6)
jour=substr(a,7,8)
heure=substr(a,9,10)
a=paste(paste(annee,mois,jour,sep="/"),paste(heure,min,sep=":"),sep=" ")
#Then I use the as.POSIXct function (as follows) that unfortunately returns NA even though the date "2013/04/02 15:00" does not correspond to the time where #the DST was #changed in France (where I am working)
> as.POSIXct(a,format="%y/%m/%d %h:%m",tz="Europe/Paris")
[1] NA
How can I solve this problem ? Much appreciated !

converting data.frames into time series

Here we go againi, I am still banging my head against the wall on the above problem.
I have a data.frame that I upload via csv which looks like:
X SPY VTI
01.02.2002 0.0000 0.0000
04.02.2002 -2.4578 -2.4167
.....
31.12.2015 -1.003 -0.9685
where X is date and SPY and VTI are stock returns
I tried many things to convert to a time series. first I tried
spyvti$X <- as.Date(as.character(spyvti$X),format="%d.%m.%Y.")
and what I get is:
X SPY VTI
NA 0.0000 0.0000
NA -2.4856 -2.4167
.....
NA -1.003 -0.9685
so it looks like it can't convert the first column, which is a factor, in an object of class(Date).
I tried also to detach the data.drame into 3 different vectors, converting first the date vector into character, which worked, then
date <- as.Date(date, format = "%d.%m.%Y.")
error in charToDate(x):
character string is not in a standard unambiguous format.
So I'd like to get some help with overcoming the Date problem, and I'd like to know if, when the date problem is over, creating a ts object as below is correct
tsobject <- xts(date,spy)
where spy is a numeric.
Thanks a lot
Paolo
Use the "lubridate" package. It makes conversion of dates super easy.
library(lubridate)
dmy(spyvti$x)
I am making this up from my mind. Hope it works. You can try the following:
Yourdataframe$X<-strptime(as.character(Yourdataframe$X),format="%d.%m.%Y")
Yourdataframe<-xts(Yourdataframe[,2:3],order.by=Yourdataframe[,1]
Assuming your example data frame is named df you can convert it into a xts time series object like so:
library(xts)
xtsObject <- as.xts(df[,-1],order.by = as.Date(as.character(df[,1]), format = "%d.%m.%Y"))

How to change the format of abbreviated month with a ending dot in my software R?

I am Peruvian and I'm learning to program in R
when I convert a character to date using the function as.Date I get NA.
This is because the abbreviated month does not have a point (Jul. Sep.)
fecha<-c("06-jul-2015","06-sep-2012")
as.Date(fecha, format="%d-%b-%Y")
[1] NA NA
Please. How I can do to R consider the abbreviated month without the point?
Thank You So Much
I had exactly the same problem yesterday. I just added the following line
Sys.setlocale("LC_TIME", "C")
fecha<-c("06-jul-2015","06-sep-2012")
as.Date(fecha, format="%d-%b-%Y")
and it works:
"2015-07-06" "2012-09-06"
Edit: for the Spanish and the dot in your comment:
Sys.setlocale("LC_TIME","Spanish")
fecha<-c("06-ene.-2015","06-dic.-2012")
as.Date(fecha, format="%d-%b.-%Y")

Operation with a date vector returns this message: Error in charToDate(x)

I have a problem with some date variables in my data. I already checked other similar questions here but I couldn't find the answer.
I have a very long dataset and some date vectors. The data was originally in stata format, I've tried to change them into R date format with:
as.Date(example$dstart)
which seems to work, after checking the class of the vector; but then I realised that apparently some cases are not in the standard unambiguous format that R requires, I realised when I was trying to convert "." into NAs, when I got this message
Error in charToDate(x) :
character string is not in a standard unambiguous format
This is an example of the data that I have:
head(sample)
dstart dstart2 dleave Ind
2005-03-20 <NA> 2005-11-19 1
2005-10-27 2006-07-07 2005-11-15 2
2000-02-29 2008-04-16 2005-03-02 3
2003-09-10 2007-07-23 2005-04-05 4
2004-04-24 2006-02-28 2005-10-17 5
2005-08-16 <NA> 2005-08-20 6
I presume that there are a few cases in the wrong format, but I don't know how to identify those cases.
Could you please advice me how to change the format of the of the date vector into an R format? I've tried this but it doesn't solve my problem.
as.Date(example$dstart, format = "%Y/%m/%d")
This has caused me some problem in my analysis when trying to sort by date, some dates are sorted before when they are obviously posterior.
A sample of the data
Your date format specification is using "/" instead of "-". If all your data is like your example, this should do it:
as.Date(example$dstart, format = "%Y-%m-%d")

Resources