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@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.

Reply via email to