subset daily to monthly in a zoo or xts

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

subset daily to monthly in a zoo or xts

Vikram Bahure
Dear R users,

I want to subset a daily zoo series according to its month, find % of "NA"
in each month.

I am finding it difficult to subset the daily dataset into monthly for the
given operation.I am planning to do this for a huge dataset.

Thanks in advance.

Regards
Vikram

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: subset daily to monthly in a zoo or xts

Achim Zeileis-4
On Mon, 23 Apr 2012, Vikram Bahure wrote:

> Dear R users,
>
> I want to subset a daily zoo series according to its month, find % of "NA"
> in each month.
>
> I am finding it difficult to subset the daily dataset into monthly for the
> given operation.I am planning to do this for a huge dataset.

Use aggregate(z, as.yearmon, ...) for aggregation to a monthly series. A
simple artificial example is:

## data with NAs
set.seed(1)
x <- rnorm(50)
x[sample(1:50, 10)] <- NA

## as zoo
z <- zoo(x, as.Date("2012-01-01") + 0:49)

## proportion of NAs
aggregate(z, as.yearmon, function(x) mean(is.na(x)))

hth,
Z

> Thanks in advance.
>
> Regards
> Vikram
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: subset daily to monthly in a zoo or xts

Enrico Schumann
In reply to this post by Vikram Bahure

Hi Vikram,

maybe something like this?

require("zoo")

## a daily zoo series
z <- zoo(1:200, as.Date("2012-01-01") + 1:200)

## add some NAs
z[ceiling(runif(10)*200)] <- NA

## aggregate to monthly
aggregate(z, format(index(z), "%Y-%m"), length)
aggregate(z, format(index(z), "%Y-%m"), function(x) sum(is.na(x)))
aggregate(z, format(index(z), "%Y-%m"), function(x) sum(is.na(x))/length(x))



Regards,
Enrico

Am 23.04.2012 11:27, schrieb Vikram Bahure:

> Dear R users,
>
> I want to subset a daily zoo series according to its month, find % of
> "NA" in each month.
>
> I am finding it difficult to subset the daily dataset into monthly
> for the given operation.I am planning to do this for a huge dataset.
>
> Thanks in advance.
>
> Regards Vikram
>
>

--
Enrico Schumann
Lucerne, Switzerland
http://nmof.net

______________________________________________
[hidden email] 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.