我有一个向量(ndx)和表(predict_all)
Vector ndx包含预测所有表的索引
ndx如下:
SELECT SUM(IIF([Date of 1st Promotion] < #1/1/2015#, 1, 0)) +
SUM(IIF([Date of 2nd Promotion] < #1/1/2015#, 1, 0)) +
SUM(IIF([Date of 3rd Promotion] < #1/1/2015#, 1, 0)) +
SUM(IIF([Date of 4th Promotion] < #1/1/2015#, 1, 0)) +
SUM(IIF([Date of 5th Promotion] < #1/1/2015#, 1, 0))
FROM [Employment History] ;
predict_all数据表如下:
V1
1
4
5
6
[...]
当我这样做时:
V1 V2 V3 V4 V5 V6
0.01 0 0.2 0.4 0.1 0
0.2 0.01 0.1 0.3 0.6 0.3
我得0.01,但如果我predict_all[1,1]
我得到1,我得0.01。
只需要解决这个问题。
答案 0 :(得分:2)
我们需要显示相同长度的行/列索引。在这里,我们试图将单元格的值设置为1,1。行索引是正确的,但列索引是data.frame
,有一列(ndx[1]
- 基于OP的帖子中显示的结构)。我们需要提取'V1'列并将第一个元素作为列索引
predict_all[1, ndx$V1[1]]
#[1] 0.01
注意:我们假设predict_all
为data.frame
如果是data.table
,请使用with = FALSE
predict_all[1, ndx$V1[1], with = FALSE]
# V1
#1: 0.01
ndx <- structure(list(V1 = c(1L, 4L, 5L, 6L)), .Names = "V1",
class = "data.frame", row.names = c(NA, -4L))
predict_all <- structure(list(V1 = c(0.01, 0.2), V2 = c(0, 0.01),
V3 = c(0.2, 0.1), V4 = c(0.4, 0.3), V5 = c(0.1, 0.6),
V6 = c(0, 0.3)), .Names = c("V1",
"V2", "V3", "V4", "V5", "V6"), class = "data.frame",
row.names = c(NA, -2L))