Sunday 15 March 2015

r - Counting and permuting elements in a list -


I have a list that looks like this:

  list.1 < - List (A = C (0.1,0.2,0.2,0.3,0.12), B = C (0.1,0.2), C = C (0.3,0.1,0.2), D = C (0.1,0.3,0.4,0.5) )   

and I would like to generate a new list with random (sorted) values ​​from list.1 , my perspective is as follows:

  rand.list & lt; - lapply repeat (list.1, fUN = function (x) (10, sample (x, 1)))   

However, if the length in the list is less than one number, For example 4, then I want to use the other elements (previous and next) in the list to calculate the ordering, and keep in mind that the length of all these elements is & gt; 4. For example, in my list, length (list.1 $ b) == 2 and length (list.1 $ c) == 3 , I like Is the random list.1 $ b value to list.1 $ a and list.1 $ c , and the random To use the list, type from $ c to the value list.1 $ b and list.1 $ d .

UPDATE

provides a solution for @dardisco:

  L2 < - list () ll1 & lt; - Length (list.1) Length (l2) & lt; - ll1 for set.seed (4) (i in 1: ll1) {vec1 & lt; - list.1 [[I]] jl & lt; - 1; Jr & lt; -1; # I have added two counter for the right one for the right time (length (vec1) <4) {if (i == 1) {vec1 & lt; - c (vec1, list.1 [[i + jr]]) jr and lt; - jr + 1} and if (i == ll1 || jr + i == ll1) {to avoid the boundaries, finally the end of several elements with about 4 elements of vec1 & lt; - c (Vec1, list.1 [[i-jl]]) jl & lt; - jl + 1} and {vec1 & lt; - c (vec1, list.1 [[i-jl]], list.1 [[i + jr]] jl & lt; - jl + 1 jr & lt; - jr + 1}} l2 [[i]]  

@ Following the advice of the Divorce - are you looking for this?

  l2 and lt; - list () length (l2) <- length (list.1) set.seed ( 1) for (i in 1: length (list.1)) {if (length (list.1 [[i]]] = 4) {l2 [[i]]   

Note that this is the first and last element in the list & = 4 elements.

Update

In the light of your comment - Start with an example that shows the problem better: < / P>

  list.1 & lt; - list (a = letters [1: 2], b = letters [3], c = letters [10:14], d = letters [25:26] ])   

then

  l2  - list () Ll1 & lt; - Length (list.1) ### ll1 = Length length 1. Length (L2) & lt; - ll1 set.seed (4) for (i in 1: ll1) {### vec1 = sample vec1 from default vector & lt; - list.1 [[i]] ### Jammu = Counter for the position relative to the current position - 1 ### If the sample size & lt; 4 (threshold) then contains additional elements in it & gt; = 4 ###f If necessary, then reduce it to below 50: As: ### While (length (vec1) <50) {while (length (vec1) <4) {## # Check if on the first element if (i == 1) {### vec1 vec1 & lt; Continue adding elements continuously from list 1 to end of c (vec1, list.1 [[i + j]]). J & lt; - j + 1 # # if on the last element, add the preceding element} and if (i == ll1) {vec1 & lt; - c (vec1, list.1 [[ij]]) j & lt; - j + 1} and {### you can add both in one step, like: ### vec1 & lt; - c (vec1, list.1 [[ij]], list.1 [[i + j]]) ### j & lt; - j + 1 ###} ### or to do this in two steps: ### ### k = Counter, point to forward or return - 1 ### If weird, the next element Add if (! K %% 2 == 0) {Vec1 & lt; -c (vec1, list.1 [[i + j]])} and {### if any, the position of the relation is vec1 & lt; - c (add preceding element and increment counter to vec1, list.1) [[Ij]]) j & lt; - j + 1} k & lt; - k + 1}} l2 [[i]] & lt; - Sample (vec1, 10, replace = TRUE)}   

This should be you, though it can be a great way. The benefits from vectorwiseness are likely to be modest in the best form.

No comments:

Post a Comment