0

Very simple question. I have a list, "lRet", of 29 vectors containing returns from the DJ index. each of the 29 stocks are observed 270 times (every day one year)

I have a matrix "DescStat" carrying 7 different descriptive statistics.

I wish to fill my matrix with for instance the mean.

DescStat <- matrix(0,29,7)

rownames(DescStat) <- mTicker$Symbol

colnames(DescStat) <- c("mean", "median", "variance", "kurtosis", "skewness", "rho", "rho2")



> head(DescStat)
    mean median variance kurtosis skewness rho rho2
TRV    0      0        0        0        0   0    0
XOM    0      0        0        0        0   0    0
V      0      0        0        0        0   0    0
PFE    0      0        0        0        0   0    0
CAT    0      0        0        0        0   0    0
IBM    0      0        0        0        0   0    0

But when I use the command my matrix looks weird when I look at it again:

DescStat[,1] <- lapply(lRet, mean)

head(DescStat)

> DescStat[,1] <- lapply(lRet, mean)
> head(DescStat)
[[1]]
[1] 0.03633877

[[2]]
[1] -0.02521246

[[3]]
[1] 0.09549176

[[4]]
[1] 0.01092361

I also tried

DescStat[,1] <- apply(lRet, 2, mean)

but I get an error dim(X) must have a positive length.

What am I doing so wrong?

Emil Krabbe
  • 101
  • 9
  • 1
    `lapply` returns a `list` and you want to fill a column of a matrix with a `list`, which is uncommon. Maybe you need `sapply` or `vapply` instead (hard to tell without knowing what `lRet` is). – nicola Oct 01 '19 at 10:27
  • "sapply" what was I needed. lRet is a list of 29 vectors/series btw – Emil Krabbe Oct 01 '19 at 10:34
  • Possible duplicate of [Grouping functions (tapply, by, aggregate) and the \*apply family](https://stackoverflow.com/questions/3505701/grouping-functions-tapply-by-aggregate-and-the-apply-family) – kath Oct 01 '19 at 10:52

0 Answers0