r - Indexing for loops in lists consisting of matrices -
i struggling indexing correctly piece of code below. perhaps set of data not ideal either.
first, want compare values between 2 columns in abcd b , d , each set of generated randomnum rows. if row value in randomnum greater b abcd value, should = 0, if else should = 1 outcome list 2 matrices, 1 comparing column b , 1 column or vector d against each set of row values (treated vectors) in randomnum using criteria described above. outcome list have 2 matrices dimensions of nv * rp , saved list udrandomnum. then, want take difference between outcome value 0 or 1 in udrandomnum row or column of 3 , each time deduct columns b , d in abcd. outcome saved in differences again have 2 matrices, each 1 comparing set of vectors of 3 against same column b , d abcd. hope makes sense.
set.seed(101) <- c(0.1,0.2,0.3) b <- c(0.8,0.2,0.5) c <- c(0.4,0.9,1.0) d <- c(0.7,0.9,0.2) ab <- cbind(a,b) cd <- cbind(c,d) abcd <- list(ab,cd) rp <- 100 nv <- length(a) randomnum <- vector("list",length(a)) # draw random values between 0 , 1 uniform distribution (i in 1:length(a)) { randomnum[[i]] <- t(replicate(rp, runif(nv, min=0,max=1))) } the problematic piece starts here:
udrandomnum <- vector("list",length(abcd)) (i in 1:length(abcd)) { udrandomnum[[i]] <- randomnum[[i]][i] < abcd[[i]][,2][col(randomnum[[i]])]+0 } # later, want take difference between outcomes (1 or 0) differences <- vector("list",length(abcd)) (i in 1:length(abcd)) { differences[[i]] <- abs(sweep(udrandomnum,2,abcd[,2])) } so randomnum have outcome starts with:
[[1]] [,1] [,2] [,3] [1,] 0.076929106 0.42883794 0.502711454 [2,] 0.254765247 0.57422550 0.578616861 [3,] 0.270195792 0.30920944 0.094095268 [4,] 0.512404975 0.97536980 0.082336057 ... i compare b: 0.8,0.2,0.5 (if value in randomnum > b 0, else 1)
udrandomnum:
[[1]] [,1] [,2] [,3] [1,] 1 0 0 [2,] 1 0 0 [3,] 1 0 1 [4,] 1 0 1 ... and deduct values in udrandomnum b outcome in differences following (whatever results typed equation convenience, should number resulting equation)
[[1]] [,1] [,2] [,3] [1,] abs(1-0.076929106) abs(0-0.42883794) abs(0-0.502711454) [2,] abs(1-0.254765247) abs(0-0.57422550) abs(0-0.578616861) [3,] abs(1-0.270195792) abs(0-0.30920944) abs(1-0.094095268) [4,] abs(1-0.512404975) abs(0-0.97536980) abs(1-0.082336057) ...
udrandomnum <- vector("list",length(abcd)) (i in 1:length(abcd)) { udrandomnum[[i]] <- randomnum[[1]] < matrix(abcd[[i]][,2][col(randomnum[[i]])], ncol=3) } differences <- vector("list",length(abcd)) (i in 1:length(abcd)) { differences[[i]] <- abs(udrandomnum[[i]] - randomnum[[i]]) } explanation
with udrandomnum list, try changing abcd b , d columns matrices match dimensions of randomnum. , differences list, r automatically coerce true , false results 1 , 0 when subtract 2 matrices udrandomnum , randomnum.
Comments
Post a Comment