R: as.POSIXct returns NA - r

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 !

Related

Character to Date

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 "-"

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".

Trimming unwanted characters

I have a very large data set (CSV) with information about bicycle counts from a bike share system. The information I'm working with is the time at which bicycles were taken out of the racks (departure time) and also the total travel time. What I want to do is to add them so I can get the arrival time at the arrival station. The departure time variable is FECHA_HORA_RETIRO and the travel time variable is TIEMPO_USO. The former, which is read by R as factor object, is in the following format: "23/01/2017 19:55:16". On the other hand, TIEMPO_USO is read by R as a character and it's in the following format: "0:17:46".
> head(viajes_ecobici_2017$FECHA_HORA_RETIRO)
[1] 28/01/2017 13:51 17/01/2017 16:24 12/01/2017 16:38 25/01/2017 10:31
> head(viajes_ecobici_2017$TIEMPO_USO)
[1] "1:35:37" "0:11:17" "0:32:51" "0:31:29" "1:31:59" "0:21:43" "0:5:43"
I first used strptime to get everything in the desired format
> viajes_ecobici_2017$FECHA_HORA_RETIRO =format(strptime(viajes_ecobici_2017$FECHA_HORA_RETIRO,format = "%d/%m/%Y %H:%M"),format = "%d/%m/%Y %H:%M:%S")
> viajes_ecobici_2017$TIEMPO_USO = format(strptime(viajes_ecobici_2017$TIEMPO_USO, format="%H:%M:%S"), format="%H:%M:%S")
This works with most observations. However, several observations became NA values after running this code. I went back to the original data to see why this was happening and created a variable with just the observations that became NA. When I looked closer at this observations I saw they have this format "\t\t01/06/2017 00:01". How can I get rid of the "\t\t" while preserving the rest of the information?
Thanks in advance for your help.
trimws() trims white space (including tab characters, \t) from the ends of a character variable:
viajes_ecobici_2017$TIEMPO_USO <- trimws(viajes_ecobici_2017$TIEMPO_USO)
For what it's worth, readr::read_csv() has a built-in trimws option (which is TRUE by default).
Assuming that the variable with the problem is TIEMPO_USO, then a simple regex would take care of the tab characters ("\t")
viajes_ecobici_2017$TIEMPO_USO <- gsub("^\\t\\t","", viajes_ecobici_2017$TIEMPO_USO)

Converting my dates into a POSIXct class

I'm currently working my way through the adehabitatLT package.
I've put my date_time column into characters and named it da:
da<-as.character(dat$date_time)
head(da)
[1] "7/08/2015 0:22" "7/08/2015 0:52" "7/08/2015 1:22" "7/08/2015 1:52" "7/08/2015 2:56" "7/08/2015 3:26"
As you can see my date_time input is a bit non traditional and i think this is where the error occurs, because when i create the class POSIXct:
da<-as.POSIXct(strptime(as.character(dat$date_time),"%d/%m/%y% H:%M:%S"))
It creates the class but i get NA for all my values:
head(da)
[1] NA NA NA NA NA NA
My end objective here is to create an object of the class ltraj (but not only containing the date but the time as well).
Any ideas anyone?
Kind regards,
Sam
da<-as.POSIXct(strptime(as.character(locs$Date),"%y%m%d"))
The format should be modified to
as.POSIXct(strptime(da, "%d/%m/%Y %H:%M"))
Or if month is first followed by day, then change it to "%m/%d/%Y %H:%M"
While parsing tricky date/time formats, it might be useful to use lubridate package by Garrett Grolemund and Hadley Wickham.
In your case, simply do
require(lubridate)
a <- dmy_hm(da)
The separator and the number of digits for day or month or hours etc are automatically parsed.
Find more info here

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"))

Resources