Home > front end >  How to update specific matrix column using an expression
How to update specific matrix column using an expression

Time:08-15

Assume we have the following list data :

dput(test)

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

I'm willing to update test[[3]][,"cumulattive_detection"] using an expression.

Assume that P=test[[1]]

Example for second row of test[[3]] , index=10 :

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

Zeros should not been considered when subsetting P , example with fifth row of test[[3]] :

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

I already tried :

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

CodePudding user response:

The error is because 22 rows are not present

> nrow(P)
[1] 19

but if this is based on the rownames, convert to character and it works

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

CodePudding user response:

Solved as explained by @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
  •  Tags:  
  • r
  • Related