[R] rollapply on zoo object

Gabor Grothendieck ggrothendieck at gmail.com
Fri Nov 30 12:22:40 CET 2007


1. rollapply is for creating zoo objects from zoo objects --
not for creating lists from zoo objects.  2. The
subscripting is wrong. 3. Its possible that you need a different
representation for out but that depends on what you want to
do with it.  At any rate try this and note that there is no
zoo processing involved:

library(zoo)
size.june <- structure(c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 20.554, 23.87,
25.62, 13.104, 28.185, 22.802, 30.033, 62.975, NA, 9.8, 13.79,
17.5, 6.49, 15.14, 12.23, 14.66, 18.38, NA, 45.92, 47.34, 45.41,
36.08, 44.44, 55, 59.4, 35.42, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, 55.4, 58.6, 49, 34, 59.43, 84.7, 87.23, 80.49, NA, 9.047,
30.2, 44.1, 56.7, 57.33, 58.17, 104.043, 98.322, 7.728, 28.476,
22.89, 20.54, 2.262, 7.514, 5.434, 4.55, 7.02, NA, 17.87, 21.51,
15.93, 11.41, 23.69, 33.2, 39.94, 42.51), .Dim = c(9L, 9L), .Dimnames = list(
    NULL, c("V2", "V3", "V4", "V5", "V6", "V7", "V8", "V9", "V10"
    )), index = structure(c(6755, 7120, 7485, 7849, 8216, 8581,
8946, 9311, 9676), class = "Date"), class = "zoo")

size.portfolio <- function(x) {
   x <- na.omit(x)
   big.size <- x[x > median(x)]
   big <- names(big.size)
   small <- setdiff(names(x), big)
   small.size <- x[small]
   list(num.of.firms = length(x), big = big, small = small,
      big.size = big.size, small.size = small.size)
}
out <- apply(size.jun, 1, size.portfolio)

On Nov 30, 2007 5:27 AM, 김상환 <picnic101 at gmail.com> wrote:
> Dear R users.
>
> I have zoo object "size_june" containing market-capital values:
>
> > dim(size_june) # market-cap data of 625 firms for 20 years
> [1]  20 625
> > class(size_june)
> [1] "zoo"
>
> > size_june  # colnames = "size.firmcode"
>
>           size.34020 size.4710 size.11050 size.10660 size.9540 size.8060
> size.16160 size.8080 size.9280
> 1988-06-30         NA        NA         NA         NA        NA
> NA         NA     7.728        NA
> 1989-06-30         NA    20.554       9.80      45.92        NA     55.40
> 9.047    28.476     17.87
> 1990-06-30         NA    23.870      13.79      47.34        NA     58.60
> 30.200    22.890     21.51
> 1991-06-29         NA    25.620      17.50      45.41        NA     49.00
> 44.100    20.540     15.93
> 1992-06-30         NA    13.104       6.49      36.08        NA     34.00
> 56.700     2.262     11.41
> 1993-06-30         NA    28.185      15.14      44.44        NA     59.43
> 57.330     7.514     23.69
> 1994-06-30         NA    22.802      12.23      55.00        NA     84.70
> 58.170     5.434     33.20
> 1995-06-30         NA    30.033      14.66      59.40        NA     87.23
> 104.043     4.550     39.94
> 1996-06-29         NA    62.975      18.38      35.42        NA     80.49
> 98.322     7.020     42.51
> ........
> ........
> (many many lines)
>
> I wrote a small function to group 625 firms into big firms and small firms:
>
> > size.portfolio
> function(data.row){
>   data.row <- data.row[,!is.na(data.row)]
>   big <- colnames(data.row[,data.row > median(coredata(data.row))]) # names
> of big firms
>   small <- colnames(data.row)[!(colnames(data.row) %in% big)] # names of
> small firms
>   big.size <- data.row[,big]
>   small.size <- data.row[,small]
>   out <- list(num.of.firms = length(data.row), big = big, small = small,
>               big.size = big.size, small.size = small.size)
> }
>
> I want to apply the function on size_june, which hopefully gives me the
> portfolio groups (big and small) every June. But the "rollapply" complains
> as below
>
> > rollapply(size_june, width=1, size.portfolio, by.column=F)
> error message about : data.row[, !is.na(data.row)] : wrong dimension
>
> So I end up using the for-loop, which produces the outcome I wanted.
> Good! No problem! But a little uneasy about using for-loop.
>
> > out <- list(list())
> > for(j in 1:length(time(size_june)))
>     out[[j]] = size.portfolio(size_june[j,])
>
> Can anyone help me with fancy codes using "apply" thing?
> Thanks in advance.
>
>        [[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.
>


More information about the R-help mailing list