如何使用表达式更新特定的矩阵列



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

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]]second row示例,index=10:

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])

按照@akrun:的解释解决

apply(test[[3]],1,function(x) return(1-prod(1-P[as.character(na.omit(x[1:4][x!=0])),as.character(x[5])])))
[1] 0.9585589 0.9585589 0.7964291 0.9585589 0.7964291 0.9585589 0.5488116 0.5488116 0.4280445 0.7964291 0.5488116 0.5488116
[13] 0.0000000 0.9081512 0.4280445 0.0000000 0.4280445 0.4280445 0.0000000

最新更新