r - How do I apply an index vector over a list of vectors? -
I apply a long index vector (50 + non-sequential integer) in a long list of vectors (50 + letters) A common example is below:
& Gt; My.list & lt; - List (C ("A", "B", "C"), C ("D", "E", "F")) My.index & lt; - 2: 3 desired output
[[1]] [1] "b" [[2]] [1] "F" # # Or [1] "b" [1] "f" ## or [1] "b" "f" I know that I can get the same value for each element By using:
& gt; Lapply (my.list, function (x) x [2]) ## or & gt; Lapply (my.list, '[', 2] I can pull the second and third values from each element:
> Lapply ( My.list, '[', my.index] [[1]] [1] "b" "c" [[2]] [1] "e" "f" for ## or & gt; (my J.in (i seq_along (my.list)) print (my.list [[i]] [[j]] [1] "b" [1] "e" [1] "c" [1 ] "F" I do not know how to pull just one value from each element.
I have not found a few days and some examples of this , But it looks quite straight forward. Do I have something clear?
Thank you, Scott < Whenever a problem that is similar to lapply but contains multiple parallel lists / vectors, With map or mapply ( map mapply with SIMPLIFY = FALSE hardcoded) A cover. Try it:
Map ("[", My.list, my.index] # [[1]] # [1] "B" ## [[2]] # [1] "F" .. or:
mapply ("[", my.list, my .index] # [1] "B" "F"
Comments
Post a Comment