Skip to content
Prev 319007 / 398503 Next

xts time series object removing time and leaving just the date

HI Douglas,
index(res)
#[1] "2012-09-10 23:59:00 EDT" "2012-09-11 23:59:00 EDT"
#[3] "2012-09-12 02:15:00 EDT"

?str(index(res))
?#POSIXct[1:3], format: "2012-09-10 23:59:00" "2012-09-11 23:59:00" ...

When you use this:

strsplit(index(res)," ")
#Error in strsplit(index(res), " ") : non-character argument


Convert it to character and split
?strsplit(as.character(index(res))," ")# split by space
#[[1]]
#[1] "2012-09-10" "23:59:00"? 

#[[2]]
#[1] "2012-09-11" "23:59:00"? 

#[[3]]
#[1] "2012-09-12" "02:15:00"? 

Now it is a list with three list elements.? Each list element is a vector of length two.

lapply(strsplit(as.character(index(res))," "), function(x) x[1]) #select the first vector element
#[[1]]
#[1] "2012-09-10"

#[[2]]
#[1] "2012-09-11"

#[[3]]
#[1] "2012-09-12"


?unlist(lapply(strsplit(as.character(index(res))," "), function(x) x[1]))? #which is still a 'character' class
#[1] "2012-09-10" "2012-09-11" "2012-09-12"


? index(res)<-unlist(lapply(strsplit(as.character(index(res))," "), function(x) x[1]))
#Error in `index<-.xts`(`*tmp*`, value = c("2012-09-10", "2012-09-11",? : 
?# unsupported ?index? index type of class ?character?

Convert it to `date`
index(res)<- as.Date(unlist(lapply(strsplit(as.character(index(res))," "), function(x) x[1])))
res
#??????????????? [,1]
#2012-09-10? 19.64393
#2012-09-11 -81.62702
#2012-09-12? 11.98883





----- Original Message -----
From: Douglas Karabasz <douglas at sigmamonster.com>
To: 'arun' <smartpink111 at yahoo.com>
Cc: 
Sent: Thursday, March 7, 2013 5:44 PM
Subject: RE: [R] xts time series object removing time and leaving just the date

Arun,

This is awesome, works perfect!? Now I'm trying to understand it.? I hate to
ask this but could you give a quick overview of 

index(res)<-as.Date(unlist(lapply(strsplit(as.character(index(res)),"
"),function(x) x[1])))? 

If I'm ask too much I understand.? You got me over the hump and I'm sure I
will understand it at a later date.? 

Thank you again,
Douglas

-----Original Message-----
From: arun [mailto:smartpink111 at yahoo.com] 
Sent: Thursday, March 07, 2013 4:04 PM
To: Douglas Karabasz
Subject: Re: [R] xts time series object removing time and leaving just the
date

Hi Douglas,
No problem.
Arun




----- Original Message -----
From: Douglas Karabasz <douglas at sigmamonster.com>
To: 'arun' <smartpink111 at yahoo.com>
Cc: 
Sent: Thursday, March 7, 2013 4:41 PM
Subject: RE: [R] xts time series object removing time and leaving just the
date

Arun,

Yes, this looks great!? Thanks for the help.? Also, thanks for creating a
xts object for me first.? I should have done it in the question looking
back.? However, I think I would have created incorrectly so I learned two
really helpful things here.

Thanks again,
Douglas

-----Original Message-----
From: arun [mailto:smartpink111 at yahoo.com]
Sent: Thursday, March 07, 2013 1:09 PM
To: Douglas Karabasz
Cc: R help
Subject: Re: [R] xts time series object removing time and leaving just the
date

Hi,

Try this:
library(xts)

Date1<- seq(as.POSIXct("2012-09-10 02:15:00",format="%Y-%m-%d %H:%M:%S"),
as.POSIXct("2012-09-12 02:15:00",format="%Y-%m-%d %H:%M:%S"), by="min")
length(Date1)
#[1] 2881
set.seed(15)
value<- rnorm(2881)
xt1<-xts(value,order.by=Date1)

res<-apply.daily(xt1,sum)
res1<- res

?res
#???????????????????????? [,1]
#2012-09-10 23:59:00? 19.64393
#2012-09-11 23:59:00 -81.62702
#2012-09-12 02:15:00? 11.98883
?index(res)<-as.Date(unlist(lapply(strsplit(as.character(index(res)),"
"),function(x) x[1])))
?res
#??????????????? [,1]
#2012-09-10? 19.64393
#2012-09-11 -81.62702
#2012-09-12? 11.98883

#or
index(res1)<-as.Date(gsub("\\s+.*","",index(res1)))
?res1
#??????????????? [,1]
#2012-09-10? 19.64393
#2012-09-11 -81.62702
#2012-09-12? 11.98883

A.K.



----- Original Message -----
From: Douglas Karabasz <douglas at sigmamonster.com>
To: r-help at r-project.org
Cc: 
Sent: Thursday, March 7, 2013 1:42 PM
Subject: [R] xts time series object removing time and leaving just the date

I have and XTS time series object that has date and time.? I started with 1
minute data and used apply.daily(x, sum) to sum the data to one cumulative
value.? This function works just fine however it leaves a time for the last
summed value which looks like this 2006-07-19 14:58:00.? I need to just have
the date and to remove the time value of 14:58:00 just leaving the date
value of 2006-07-19? .? I want to keep the xts object otherwise intact.? Is
this possible?? ? 

Thank you,
Douglas






??? [[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.