r - pmax function on lists of lists -
i have lists stored in lists. third level contains vector numeric values , nas.
i want use pmax find highest value between 2 vectors have same position.
pmax(low_all_res[[1]][[1]][[1]], high_all_res[[1]][[1]][[1]])
this works , outputs list. want lot of lists different indices. want compare low_all_res , high_all_res , highest value between them.
my data structure looks this:
low_all_res[[1:5]][[1:5]][[1:10]]
and lists in last list want compare between low , high.
i tried:
comb_all_res <- list() (k in 1:5){ for(m in 1:5){ (i in 1:10){ comb_all_res[[k]][[m]][[i]] <- pmax(low_all_res[[k]][[m]][[i]], high_all_res[[k]][[m]][[i]]) }}} error in `*tmp*`[[k]] : subscript out of bounds
but error shown.
i have tried approach temporary files, think use wrong indexing operators tmp files. code looks this, have tried lot of different approaches. code works, results last 10 vectors.
comb_all_res <- list() tmp_low <- list() tmp_high <- list() tmp_low2 <- list() tmp_high2 <- list() (i in 1:10){ (k in 1:5){ tmp_low <- low_all_res[[k]] tmp_high <- high_all_res[[k]] for(m in 1:5){ tmp_low2 <- tmp_low[[m]] tmp_high2 <- tmp_high[[m]] comb_all_res[[i]] <- pmax(tmp_low2[[i]], tmp_high2[[i]]) }}}
can critique code, can straight?
you try following:
# fake data: set.seed(123) list_1 <- list(list(list(1:5), list(rep(3,5)), list(sample(1:10, 5))), list(list(sample(10:15)), list(rep(10,5)), list(sample(100:110, 5)))) list_2 <- list(list(list(5:1), list(rep(5,5)), list(sample(10:1, 5))), list(list(sample(15:10)), list(rep(20,5)), list(sample(1000:110, 5)))) pmax(unlist(list_1), unlist(list_2)) [1] 5 4 3 4 5 5 5 5 5 5 8 10 4 7 6 11 12 13 14 15 15 20 20 [24] 20 20 20 471 743 870 145 200
to put results list of similar structure original lists can following (thanks ananda mahto helped find relist
):
relist(pmax(unlist(list_1), unlist(list_2)), skeleton = list_1) [[1]] [[1]][[1]] [[1]][[1]][[1]] [1] 5 4 3 4 5 [[1]][[2]] [[1]][[2]][[1]] [1] 5 5 5 5 5 [[1]][[3]] [[1]][[3]][[1]] [1] 8 10 4 7 6 [[2]] [[2]][[1]] [[2]][[1]][[1]] [1] 11 12 13 14 15 15 [[2]][[2]] [[2]][[2]][[1]] [1] 20 20 20 20 20 [[2]][[3]] [[2]][[3]][[1]] [1] 471 743 870 145 200
Comments
Post a Comment