I have a chr variable in formats:
20160101 000000
20160101 235600
which I want to convert to:
2016-01-01 00:00:00
2016-01-01 23:56:00
I can convert the date easily with:
as.Date('20160101 000000', format="%Y%m%d")
but that only converts the date. So I wonder if there is a function that can do both.
Cheers
Update:
Per suggestion of David Arenburg I use:
as.POSIXct('20160101 000000', format='%Y%m%d %H%M%S')
and this solves my problem.
Related
I have two cloumns one of them is date(it is date format 2020-06-17), another one is time of transaction (it is integer 1258) how I can concat them as Timestamp?
Example
Date Time Expected_Result
2020-06-17 1258 2020-06-17 12:58:00
2020-08-15 2315 2020-08-15 23:15:00
2020-10-10 1238 2020-10-10 12:38:00
Multiple approaches are possible. One would be convert everything to string first:
cast(cast(cast(d as format 'yyyy-mm-dd') as varchar(11))||' '||cast(cast(t as format '99:99') as varchar(10))||':00' as timestamp(0))
Another possibility is to split up hours and minutes using integer arithmetic and convert units individually:
cast(d as timestamp(0))+ (t/100)*interval '1' hour + (t mod 100)*interval '1' minute
When I am converting dates from characters to "dates" it seems to be off by 2 days from excel?
My example
mydata <- c(38808,40422,40493,40606)
as.Date(mydata, origin="1900-01-01")
# [1] "2006-04-03" "2010-09-03" "2010-11-13" "2011-03-06"
yet in excel the dates are as follows
Date in Excel in R Delta
38808 2006-04-01 2006-04-03 2
40422 2010-09-01 2010-09-03 2
40493 2010-11-11 2010-11-13 2
40606 2011-03-04 2011-03-06 2
I get around it by changing origin date to 1899-12-30 but I am sure I am doing something wrong.
Thanks
It is a known problem that Excel thinks 1900 was a leap year, but it was not. So Excel counts an extra day (for nonexistent Feb 29, 1900). In addition, Excel considers "1900-01-01" as day 1, not day 0.
Maybe the link will help:
http://www.cpearson.com/excel/datetime.htm
For excel dates you need this one:
mydata <- c(38808,40422,40493,40606)
as.Date(mydata, origin = "1899-12-30")
[1] "2006-04-01" "2010-09-01" "2010-11-11" "2011-03-04"
I have a large dataset corresponding to different sites and timezone.
I'd like to standardize all of the sites to "UCT". I'm struggling to transform the dates (which are a factor) to get the proper date format.
One small sample of my date looks like this:
head(data_tz)
site DatetimeEnd tzone
FR01001 2014-10-28 00:00:00 UTC
FR01001 2014-11-02 00:00:00 UTC
FR01001 2014-01-20 00:00:00 UTC
FR01001 2014-11-01 00:00:00 UTC
FR01001 2014-01-13 00:00:00 UTC
FR01001 2014-09-17 00:00:00 UTC
..........
This is a large dataset with 4 different tzone:
unique(data_tz$tzone)
"UTC" "UTC-04" "UTC+04" "UTC-03"
And DatetimeEnd is a factor, which I need to convert to POSIXct, and then each site to "UTC". I have been trying different approaches but neither of them worked.
I am using:
newdata$DatetimeEnd <- as.POSIXct(data_tz$DatetimeEnd, format="%Y-%m-%d %H:%M:%S",tz=data_tz$tzone)
But I got:
Error in strptime(x, format, tz = tz) : invalid 'tz' value
And the same when using:
newdata$DatetimeEnd <- as.POSIXct(strptime(data_tz$DatetimeEnd,
format="%Y-%m-%d %H:%M:%S",tz=data_tz$tzone))
If I use:
newdata$DatetimeEnd <- as.POSIXct(data_tz$DatetimeEnd, format="%Y-%m-%d %H:%M:%S",tz="UTC +01")
It works, but it is not what I want, since there are some columns (sites) with "UCT +02"..(different tzone)
How can I use here the tz as an argument to get the right timezone? Any idea/suggestion would be really helpful.
Thanks
You can use purrr::map2 to iterate over the rows of the columns DatetimeEnd and tzone, creating a new vector.
I have data of date and I want to extract the month's name and its year:
>head(merged.tables$Date)
2015-07-31
2013-01-12
2014-01-03
2014-12-03
2013-11-13
2013-10-27
In other word I need to get the result arranged in ascending order like this:
January_2013
October_2013
November_2013
January_2014
December_2014
July_2015
So I tried to use this code to convert first as shown in the code above.
merged.tables$Date <-paste(months(as.Date(merged.tables$Date)),year(as.Date(merged.tables$Date)),sep="_")
But It show me this error:
Error in charToDate(x) :
character string is not in a standard unambiguous format
I tried to adapt this solution character string is not in a standard unambiguous format.
But I can't resolve it!
Thank you for your help
You can use order on the original dates to get the correct order.
First, read in your data example.
Date <- scan(what = character(), text = '
2015-07-31
2013-01-12
2014-01-03
2014-12-03
2013-11-13
2013-10-27')
Date <- as.Date(Date)
Now, reformat and order them.
inx <- order(Date)
newDate <- format(Date, format = "%B_%Y")[inx]
newDate
#[1] "January_2013" "October_2013" "November_2013" "January_2014"
#[5] "December_2014" "July_2015"
Note that this assumes that the dates are of class Date.
Try this (passing your date to character):
merged.tables$Date <-paste(months(as.Date(as.character(merged.tables$Date))),year(as.Date(as.character(merged.tables$Date))),sep="_")
If you build your data.frame like this, you don't have that error:
merged.tables<-data.frame(Date=c("2015-07-31","2013-01-12",
"2014-01-03",
"2014-12-03",
"2013-11-13",
"2013-10-27"))
I suppose that the problem is related to the class of your data.
Ordering:
merged.tables$Date2 <-paste(months(as.Date(as.character(merged.tables$Date))),year(as.Date(as.character(merged.tables$Date))),sep="_")
merged.tables[order(merged.tables$Date),]
Date Date2
2 2013-01-12 gennaio_2013
6 2013-10-27 ottobre_2013
5 2013-11-13 novembre_2013
3 2014-01-03 gennaio_2014
4 2014-12-03 dicembre_2014
1 2015-07-31 luglio_2015
Below is my R-code that is behaving weirdly. I expect a time of 22:00 as entered but i get 23:00.
as.POSIXct(chron(dates="01/04/06",times="22:00:00"),tz="CET")
[1] "2006-01-04 23:00:00 CET"
In the next line of my code i use the results to select a window from an xts/zoo object: Therefore just ignoring the error and instead entering 21:00 (in above) wasnt useful since it returns the wrong data. Windowing with the result of the code above returns the correct values.
head(qs<-as.zoo(window(Q,start=as.POSIXct(chron(dates="01/04/06",times="22:00:00"),tz="CET"),end=as.POSIXct(chron(dates="01/05/06",times="21:00:00"),tz="CET"))))
Here is a sample set of the data (Q):
Stage.Qm Flow.Qm Stage.QmDB Flow.QmDB Stage.Q1000 Flow.Q1000 Stage.Q1000DB Flow.Q1000DB
2006-01-04 23:00:00 541.1589 5.636957 541.1592 5.646017 541.5708 20.44692 541.5708 20.44692
2006-01-04 23:01:00 541.1589 5.637268 541.1592 5.645087 541.5701 20.41321 541.5701 20.41321
2006-01-04 23:02:00 541.1589 5.638604 541.1588 5.635806 541.5701 20.40946 541.5701 20.40946
2006-01-04 23:03:00 541.1589 5.638979 541.1588 5.635694 541.5704 20.42712 541.5704 20.42712
2006-01-04 23:04:00 541.1589 5.639619 541.1590 5.640691 541.5710 20.45848 541.5710 20.45848
2006-01-04 23:05:00 541.1590 5.640662 541.1591 5.641682 541.5715 20.47893 541.5715 20.4789
In the documentation you can read: "The current implementation of chron objects does not handle time zones nor daylight savings time." Thus, a solution would be to not use chron here.
Just use as.POSIXct.default:
as.POSIXct("2006-01-04 22:00:00", "%Y-%m-%d %H:%M:%S", tz="CET")
[1] "2006-01-04 22:00:00 CET"