r - Unexpected behavior with non-nested looping over multiple lists -



r - Unexpected behavior with non-nested looping over multiple lists -

i want produce bunch of character vectors like

[1] "w2q5ea" "w2q5eb" "w2q5ec" "w2q5ed"

i have next setup:

vars <- list( w1 = c("w2q5e*","w2q7e*"), w2 = c("w3q9*5","w3q13*5","w3q15*5"), w3 = c("w4q17*c","w4q16*c","w4q15*c"), w4 = c("w5q16*c","w5q14*c","w5q11*c"), w5 = c("w5q8*c")) alphabet <- function(n) lapply(n,function(n) letters[1:n]) nletts <- list( w1 = list(unlist(alphabet(10)),unlist(alphabet(10))), w2 = list(unlist(alphabet(10)),unlist(alphabet(10)),unlist(alphabet(10))), w3 = list(unlist(alphabet(10)),unlist(alphabet(6)),unlist(alphabet(6))), w4 = list(unlist(alphabet(6)),unlist(alphabet(6)),unlist(alphabet(6))), w5 = list(unlist(alphabet(6))))

so that, e.g.,

> nletts[["w1"]] [[1]] [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" [[2]] [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"

in order produce looks like

[1] "w2q5ea" "w2q5eb" "w2q5ec" "w2q5ed" [1] "w2q7ea" "w2q7eb" "w2q7ec" "w2q7ed"

and that's thought next code do:

temp <- list() for( in 1:2 ){ letts <- unlist(nletts[["w1"]][[i]]) for(l in seq_along(nletts)) for( v in vars[["w1"]] ) { temp <- append(temp,sub("\\*",letts[l],v)) } }

but apparently it's not right. didn't print result sake of making post somewhat readable, can run have , see yourself. reason seems looping 3 times , not once. suspect something, somewhere, vectorized in way i'm missing, , life of me can't figure out is. i've rewritten code on , on 4 hours straight , exact same problem each time. what's odd beyond comprehension can replace l integer , step through hand right result.

forget trying original job had in mind, have finished 3 hours ago re-create & paste. want understand what's going wrong. , whole thing feels hacky , bad begin with, alternative suggestions welcome.

got work with

for( in seq_along(vars[["w1"]]) ){ print(sprintf(vars[["w1"]][i],letters[ nletts[["w1"]][i] ] )) }

but still want know went wrong above. think happening looping on each list element of nletts twice. realize loops nested though didn't want them be, maybe problem lies in there somewhere. using same index simultaneously fixed issue.

better solution doesn't require append:

whatever <- sapply(1:5, function(w){ sapply( seq_along(vars[[w]]), function(i){ sprintf(vars[[w]][i],letters[ 1:nletts[[w]][i] ] ) },simplify=f) },simplify=f)

but still no explanation went wrong.

how trying recipe:

r> vars <- c("w2q5e%s", "w5q8%sc") r> lapply(vars, function (x) sprintf(x, letters)) [[1]] [1] "w2q5ea" "w2q5eb" "w2q5ec" "w2q5ed" "w2q5ee" "w2q5ef" "w2q5eg" "w2q5eh" "w2q5ei" [10] "w2q5ej" "w2q5ek" "w2q5el" "w2q5em" "w2q5en" "w2q5eo" "w2q5ep" "w2q5eq" "w2q5er" [19] "w2q5es" "w2q5et" "w2q5eu" "w2q5ev" "w2q5ew" "w2q5ex" "w2q5ey" "w2q5ez" [[2]] [1] "w5q8ac" "w5q8bc" "w5q8cc" "w5q8dc" "w5q8ec" "w5q8fc" "w5q8gc" "w5q8hc" "w5q8ic" [10] "w5q8jc" "w5q8kc" "w5q8lc" "w5q8mc" "w5q8nc" "w5q8oc" "w5q8pc" "w5q8qc" "w5q8rc" [19] "w5q8sc" "w5q8tc" "w5q8uc" "w5q8vc" "w5q8wc" "w5q8xc" "w5q8yc" "w5q8zc"

r for-loop vectorization

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -