r - Match number within list of different length vectors -
I want to match a number within the vector list of different lengths. Nevertheless, my solution (nothing below) does not match anything beyond the first item of each vector.
seq_ & lt; - seq (1:10) list_ & lt; - List [seq_ [1: 3]], Seq_ [4: 7], seq_ [8:10]) List_ # [[1]] # [1] 1 2 3 # # [[2]] # [1] 4 5 6 7 # # [[3]] # [1] 8 9 10 but
for (i seq_) {print (match) (I, list_)) # [1] 1 # [1] NA # [1] NA # [1] 3 # [1] N # [1] NA # [1] N # [1] NA # [1] NA # [1] NA which
edit: rewrite to show full looping on values.
seq_ & lt; - seq (1:10) list_ & lt; - List (seq_ [1: 3], seq_ [4: 7], seq_ [8:10]) matchlist & lt; -list (length = length (list_)) in (j in 1: length (list_)) {matchlist [[j]] & lt; - List (sapply (seq_, function (k) which (list _ [[[j]] == k)} He will return the position of all matches if you select my.list & lt; - list (sample (1: 10, 4, replace = TRUE), create an input such as sample (1: 10,7, replacement = TRUE), then this is probably becoming more obvious < / Code>
Comments
Post a Comment