Victor,
The "weekdays" function will return the days of the week (as a character
vector of names) that a given vector of dates (Date or POSIXct) fall on.
These can then be converted into numbers using a look-up table/vector. See
below for an example using the sample_matrix data included with the xts
package.
##
require(xts)
data(sample_matrix)
dates <- as.POSIXct(rownames(sample_matrix), format = "%Y-%m-%d")
dayLookup <- 1:7
names(dayLookup) <- c("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun")
datesDays <- dayLookup[weekdays(dates, abbreviate = TRUE)]
print(datesDays)
##
>From here, you can just add the "datesDays" vector as an additional column
to the original data, e.g.
xts(cbind(sample_matrix, dow =datesDays), order.by = dates).
HTH,
Francisco
On Mon, Mar 7, 2011 at 9:05 AM, Victor <[email protected]> wrote:
> I have the following xts objetct "temp"
>
> > str(temp)
> An xts object from 2010-12-26 to 2011-03-05 containing:
> Data: num [1:70, 1] 2.95 0.852 -0.139 1.347 2.485 ...
> - attr(*, "dimnames")=List of 2
> ..$ : NULL
> ..$ : chr "t_n"
> Indexed by objects of class: [POSIXct,POSIXt] TZ: GMT
> xts Attributes:
> NULL
>
>
> > temp
> t_n
> 2010-12-26 2.9500000
> 2010-12-27 0.8520833
> 2010-12-28 -0.1390625
> ...........
>
> I would like to associate another column with the day of week in the form
> of 1=Mon, 2=Tue, ..., 7=Sun
> in order to obtain:
>
> >newtemp
>
> t_n dow
> 2010-12-26 2.9500000 7
> 2010-12-27 0.8520833 1
> 2010-12-28 -0.1390625 2
> ..............
>
> How could make this in the shortest (and elegant?) way?
>
> Ciao from Rome
> Vittorio
>
> ______________________________________________
> [email protected] 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.
>
[[alternative HTML version deleted]]
______________________________________________
[email protected] 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.