假设我们有以下列表数据:

DPUT(测试)

list(structure(c(0, 0.548811636094026, 0.301194211912202, 0.149963012878635, 
0.428044491190235, 0.548811636094026, 0.428044491190235, 0.261416388017453, 
0.149963012878635, 0.428044491190235, 0.548811636094026, 0.428044491190235, 
0.261416388017453, 0.136698860068783, 0.229995888646225, 0.353726775477146, 
0.428044491190235, 0.353726775477146, 0.229995888646225, 0.548811636094026, 
0, 0.548811636094026, 0.0842587063712626, 0.261416388017453, 
0.428044491190235, 0.548811636094026, 0.428044491190235, 0.0842587063712626, 
0.261416388017453, 0.428044491190235, 0.548811636094026, 0.428044491190235, 
0.0784272047642984, 0.136698860068783, 0.229995888646225, 0.353726775477146, 
0.428044491190235, 0.353726775477146, 0.301194211912202, 0.548811636094026, 
0, 0.0469152869838949, 0.149963012878635, 0.261416388017453, 
0.428044491190235, 0.548811636094026, 0.0469152869838949, 0.149963012878635, 
0.261416388017453, 0.428044491190235, 0.548811636094026, 0.0442592250467268, 
0.0784272047642984, 0.136698860068783, 0.229995888646225, 0.353726775477146, 
0.428044491190235, 0.149963012878635, 0.0842587063712626, 0.0469152869838949, 
0, 0.301194211912202, 0.165298888221587, 0.0907179532894125, 
0.0497870683678639, 0.428044491190235, 0.229995888646225, 0.136698860068783, 
0.0784272047642984, 0.0442592250467268, 0.548811636094026, 0.428044491190235, 
0.261416388017453, 0.149963012878635, 0.0842587063712626, 0.0469152869838949, 
0.428044491190235, 0.261416388017453, 0.149963012878635, 0.301194211912202, 
0, 0.548811636094026, 0.301194211912202, 0.165298888221587, 0.229995888646225, 
0.428044491190235, 0.353726775477146, 0.229995888646225, 0.136698860068783, 
0.261416388017453, 0.428044491190235, 0.548811636094026, 0.428044491190235, 
0.261416388017453, 0.149963012878635, 0.548811636094026, 0.428044491190235, 
0.261416388017453, 0.165298888221587, 0.548811636094026, 0, 0.548811636094026, 
0.301194211912202, 0.136698860068783, 0.353726775477146, 0.428044491190235, 
0.353726775477146, 0.229995888646225, 0.149963012878635, 0.261416388017453, 
0.428044491190235, 0.548811636094026, 0.428044491190235, 0.261416388017453, 
0.428044491190235, 0.548811636094026, 0.428044491190235, 0.0907179532894125, 
0.301194211912202, 0.548811636094026, 0, 0.548811636094026, 0.0784272047642984, 
0.229995888646225, 0.353726775477146, 0.428044491190235, 0.353726775477146, 
0.0842587063712626, 0.149963012878635, 0.261416388017453, 0.428044491190235, 
0.548811636094026, 0.428044491190235, 0.261416388017453, 0.428044491190235, 
0.548811636094026, 0.0497870683678639, 0.165298888221587, 0.301194211912202, 
0.548811636094026, 0, 0.0442592250467268, 0.136698860068783, 
0.229995888646225, 0.353726775477146, 0.428044491190235, 0.0469152869838949, 
0.0842587063712626, 0.149963012878635, 0.261416388017453, 0.428044491190235, 
0.548811636094026, 0.149963012878635, 0.0842587063712626, 0.0469152869838949, 
0.428044491190235, 0.229995888646225, 0.136698860068783, 0.0784272047642984, 
0.0442592250467268, 0, 0.301194211912202, 0.165298888221587, 
0.0907179532894125, 0.0497870683678639, 0.548811636094026, 0.428044491190235, 
0.261416388017453, 0.149963012878635, 0.0842587063712626, 0.0469152869838949, 
0.428044491190235, 0.261416388017453, 0.149963012878635, 0.229995888646225, 
0.428044491190235, 0.353726775477146, 0.229995888646225, 0.136698860068783, 
0.301194211912202, 0, 0.548811636094026, 0.301194211912202, 0.165298888221587, 
0.261416388017453, 0.428044491190235, 0.548811636094026, 0.428044491190235, 
0.261416388017453, 0.149963012878635, 0.548811636094026, 0.428044491190235, 
0.261416388017453, 0.136698860068783, 0.353726775477146, 0.428044491190235, 
0.353726775477146, 0.229995888646225, 0.165298888221587, 0.548811636094026, 
0, 0.548811636094026, 0.301194211912202, 0.149963012878635, 0.261416388017453, 
0.428044491190235, 0.548811636094026, 0.428044491190235, 0.261416388017453, 
0.428044491190235, 0.548811636094026, 0.428044491190235, 0.0784272047642984, 
0.229995888646225, 0.353726775477146, 0.428044491190235, 0.353726775477146, 
0.0907179532894125, 0.301194211912202, 0.548811636094026, 0, 
0.548811636094026, 0.0842587063712626, 0.149963012878635, 0.261416388017453, 
0.428044491190235, 0.548811636094026, 0.428044491190235, 0.261416388017453, 
0.428044491190235, 0.548811636094026, 0.0442592250467268, 0.136698860068783, 
0.229995888646225, 0.353726775477146, 0.428044491190235, 0.0497870683678639, 
0.165298888221587, 0.301194211912202, 0.548811636094026, 0, 0.0469152869838949, 
0.0842587063712626, 0.149963012878635, 0.261416388017453, 0.428044491190235, 
0.548811636094026, 0.136698860068783, 0.0784272047642984, 0.0442592250467268, 
0.548811636094026, 0.261416388017453, 0.149963012878635, 0.0842587063712626, 
0.0469152869838949, 0.548811636094026, 0.261416388017453, 0.149963012878635, 
0.0842587063712626, 0.0469152869838949, 0, 0.548811636094026, 
0.301194211912202, 0.165298888221587, 0.0907179532894125, 0.0497870683678639, 
0.229995888646225, 0.136698860068783, 0.0784272047642984, 0.428044491190235, 
0.428044491190235, 0.261416388017453, 0.149963012878635, 0.0842587063712626, 
0.428044491190235, 0.428044491190235, 0.261416388017453, 0.149963012878635, 
0.0842587063712626, 0.548811636094026, 0, 0.548811636094026, 
0.301194211912202, 0.165298888221587, 0.0907179532894125, 0.353726775477146, 
0.229995888646225, 0.136698860068783, 0.261416388017453, 0.548811636094026, 
0.428044491190235, 0.261416388017453, 0.149963012878635, 0.261416388017453, 
0.548811636094026, 0.428044491190235, 0.261416388017453, 0.149963012878635, 
0.301194211912202, 0.548811636094026, 0, 0.548811636094026, 0.301194211912202, 
0.165298888221587, 0.428044491190235, 0.353726775477146, 0.229995888646225, 
0.149963012878635, 0.428044491190235, 0.548811636094026, 0.428044491190235, 
0.261416388017453, 0.149963012878635, 0.428044491190235, 0.548811636094026, 
0.428044491190235, 0.261416388017453, 0.165298888221587, 0.301194211912202, 
0.548811636094026, 0, 0.548811636094026, 0.301194211912202, 0.353726775477146, 
0.428044491190235, 0.353726775477146, 0.0842587063712626, 0.261416388017453, 
0.428044491190235, 0.548811636094026, 0.428044491190235, 0.0842587063712626, 
0.261416388017453, 0.428044491190235, 0.548811636094026, 0.428044491190235, 
0.0907179532894125, 0.165298888221587, 0.301194211912202, 0.548811636094026, 
0, 0.548811636094026, 0.229995888646225, 0.353726775477146, 0.428044491190235, 
0.0469152869838949, 0.149963012878635, 0.261416388017453, 0.428044491190235, 
0.548811636094026, 0.0469152869838949, 0.149963012878635, 0.261416388017453, 
0.428044491190235, 0.548811636094026, 0.0497870683678639, 0.0907179532894125, 
0.165298888221587, 0.301194211912202, 0.548811636094026, 0), .Dim = c(19L, 
19L), .Dimnames = list(c("4", "5", "6", "7", "9", "10", "11", 
"12", "13", "15", "16", "17", "18", "19", "20", "21", "22", "23", 
"24"), c("4", "5", "6", "7", "9", "10", "11", "12", "13", "15", 
"16", "17", "18", "19", "20", "21", "22", "23", "24"))), structure(c(4, 
4, 5, 4, 5, 9, 10, 11, 5, 5, 6, 6, 12, 7, 10, 16, 19, 19, 19, 
6, 9, 10, 15, 16, 15, 16, 17, 10, 12, 11, 17, 18, 13, 21, 21, 
21, 13, 7, 11, 11, 12, 17, 18, 20, 21, 22, 16, 18, 24, 24, 23, 
20, 4, 4, 7, 20, 20, 17, 22, 23, 22, 23, 22, 23, 24, 9, 11, 5, 
5, 6, 21, 15, 9, 9, 9, 15, 5, 10, 11, 16, 17, 21, 22, 23, 4, 
6, 12, 18, 24, 19, 9, 15, 20, 7, 13, 0.548811636094026, 0.548811636094026, 
0.548811636094026, 0.548811636094026, 0.548811636094026, 0.548811636094026, 
0.548811636094026, 0.548811636094026, 0.548811636094026, 0.548811636094026, 
0.548811636094026, 0.548811636094026, 0.548811636094026, 0.548811636094026, 
0.548811636094026, 0.548811636094026, 0.548811636094026, 0.548811636094026, 
0.548811636094026, 0.548811636094026, 0.548811636094026, 0.548811636094026, 
0.548811636094026, 0.548811636094026, 0.548811636094026, 0.548811636094026, 
0.548811636094026, 0.548811636094026, 0.548811636094026, 0.548811636094026, 
0.548811636094026, 0.548811636094026, 0.548811636094026, 0.548811636094026, 
0.548811636094026, 0.548811636094026, 0.428044491190235, 0.428044491190235, 
0.548811636094026, 0.548811636094026, 0.548811636094026, 0.548811636094026, 
0.548811636094026, 0.548811636094026, 0.548811636094026, 0.548811636094026, 
0.548811636094026, 0.548811636094026, 0.548811636094026, 0.548811636094026, 
0.548811636094026, 0.548811636094026, 0.428044491190235, 0.428044491190235, 
0.428044491190235, 0.428044491190235, 0.428044491190235, 0.548811636094026, 
0.548811636094026, 0.548811636094026, 0.548811636094026, 0.548811636094026, 
0.548811636094026, 0.548811636094026, 0.548811636094026, 0.428044491190235, 
0.428044491190235, 0.428044491190235, 0.428044491190235, 0.428044491190235, 
0.301194211912202, 0.428044491190235, 0.428044491190235, 0.428044491190235, 
0.301194211912202, 0.301194211912202, 0.958558872499827, 0.958558872499827, 
0.958558872499827, 0.958558872499827, 0.958558872499827, 0.958558872499827, 
0.958558872499827, 0.958558872499827, 0.947466550422937, 0.947466550422937, 
0.947466550422937, 0.947466550422937, 0.947466550422937, 0.935815499514876, 
0.933405206592068, 0.933405206592068, 0.933405206592068, 0.896857209066517, 
0.896857209066517), .Dim = c(19L, 10L), .Dimnames = list(NULL, 
    c("", "", "", "", "index", "", "", "", "", "cumulattive_detection"
    ))), structure(c(4, 4, 0, 4, 0, 9, 0, 0, 0, 0, 0, 0, 0, 7, 
0, 0, 0, 0, 0, 6, 9, 0, 15, 0, 15, 0, 0, 0, 12, 0, 0, 0, 13, 
0, 0, 0, 13, 0, 11, 11, 12, 17, 18, 20, 0, 0, 0, 18, 24, 24, 
0, 20, 0, 0, 7, 0, 0, 17, 22, 23, 22, 23, 22, 23, 24, 9, 0, 0, 
0, 0, 0, 15, 0, 0, 0, 0, 5, 10, 11, 16, 17, 21, 22, 23, 4, 6, 
12, 18, 24, 19, 9, 15, 20, 7, 13, 0.958558872499827, 0.958558872499827, 
0.958558872499827, 0.958558872499827, 0.958558872499827, 0.958558872499827, 
0.958558872499827, 0.958558872499827, 0.947466550422937, 0.947466550422937, 
0.947466550422937, 0.947466550422937, 0.947466550422937, 0.935815499514876, 
0.933405206592068, 0.933405206592068, 0.933405206592068, 0.896857209066517, 
0.896857209066517, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 
0, 0, 0, 0, 0), .Dim = c(19L, 7L), .Dimnames = list(NULL, c("", 
"", "", "", "index", "cumulattive_detection", "Solution_selected"
))))

我愿意使用一个表达式更新test[[3]][,"cumulattive_detection"].

假设P=test[[1]]

test[[3]],index=10中的second row个示例:

test[[3]][2,"cumulattive_detection"]=1-prod(1-P[c(4,9,11,22),10])

在设置P的子集时不应考虑零,例如test[[3]]中的fifth row:

test[[3]][5,"cumulattive_detection"]=1-prod(1-P[c(18,23),17])

我已经试过了:

P=test[[1]]
test[[3]][,"cumulattive_detection"]=apply(test[[3]],1,function(x) return(1-prod(1-P[as.numeric(na.omit(x[1:4][x!=0])),x[5]])))

Error in P[as.numeric(na.omit(x[1:4][x != 0])), x[5]] : 
  subscript out of bounds

推荐答案

错误是因为不存在22行

> nrow(P)
[1] 19

但如果这是基于行名,转换为character就可以了

test[[3]][2,"cumulattive_detection"]=1-prod(1-P[as.character(c(4,9,11,22)),10])
test[[3]][5,"cumulattive_detection"]=1-prod(1-P[as.character(c(18,23)),17])

R相关问答推荐

混淆矩阵,其中每列和等于1

如果行和列名以相同的开头,将矩阵值设置为0

过滤矩阵以获得R中的唯一组合

格点中指数、双曲和反双曲模型曲线的正确绘制

如何计算多个日期是否在一个日期范围内

我如何才能找到FAMILY=POISSON(LINK=&Q;LOG&Q;)中的模型预测指定值的日期?

在数组索引上复制矩阵时出错

基于R中的间隔扩展数据集行

以NA为通配符的R中的FULL_JOIN以匹配其他数据中的任何值.Frame

如何将使用rhandsontable呈现的表值格式化为百分比,同时保留并显示完整的小数精度?

自定义gggraph,使geom_abline图层仅在沿x轴的特定范围内显示

如何从向量构造一系列双边公式

如何将一列中的值拆分到R中各自的列中

自动STAT_SUMMARY统计与手动标准误差之间的差异

减go R中列表的所有唯一元素对

在R中,如何将误差条放置在堆叠的每个条上?

如何使用字符串从重复的模式中提取多个数字?

创建在文本字符串中发现两个不同关键字的实例的数据框

基于R中的辅助向量中的值有条件地连接向量中的字符串

R将函数参数传递给ggploy