这是我的代码:
indices <- as.matrix(sapply(cpi_calc[2:nrow(cpi_calc), 1:ncol(cpi_calc)], as.numeric))
weigths <- as.matrix(sapply(cpi_calc[1, 1:ncol(cpi_calc)], as.numeric))
CPI77_82 <- indices %*% weights
我想用索引矩阵和权重向量计算CPI。矩阵和向量在变量窗口中显示为num。我不知道为什么矩阵乘法不起作用。
Error in indices %*% weights :
requires numeric/complex matrix/vector arguments
我知道还有很多其他类似的问题,但我的代码似乎对我很好,我尝试了其他类似问题的所有答案。请帮忙! :)
修改
> cpi_calc[2:6, 1:6]
# A tibble: 5 x 6
X__1 X__2 X__3 X__4 X__5 X__6
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 100.00 100.00 100.00 100.00 100.00 100.00
2 99.99 100.00 100.07 100.01 100.80 101.59
3 99.52 99.58 99.94 100.01 101.03 101.38
4 99.46 99.44 99.85 100.01 101.03 101.03
5 99.13 99.37 99.79 99.97 101.00 101.82
> t(cpi_calc[1, 1:6])
[,1]
X__1 0.358
X__2 0.359
X__3 0.060
X__4 0.419
X__5 0.191
X__6 0.296
答案 0 :(得分:0)
这很好用。您的cpi_calc表中必须有一些NA。试试na.omit(cpi_calc)
cpi_calc <- read.table(text="0.358 0.359 0.06 0.419 0.191 0.296
100 100 100 100 100 100
99.99 100 100.07 100.01 100.8 101.59
99.52 99.58 99.94 100.01 101.03 101.38
99.46 99.44 99.85 100.01 101.03 101.03
99.13 99.37 99.79 99.97 101 101.82",header=FALSE)
as.matrix(cpi_calc[2:6, 1:6]) %*% t(cpi_calc[1, 1:6])
1
2 168.3000
3 168.9282
4 168.5832
5 168.4024
6 168.4669