Q:

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 (permuted) values from list.1, my approach is the following:
rand.list <- lapply(list.1, FUN=function(x) replicate(10, sample(x,1)))

However, if the length of an element in the list is less than a number, e.g 4, then I would like to use also other elements (previous and next) in the list for calculating the permutations, taking in account that the length of all these elements is > 4. For example, in my list, length(list.1$b) == 2 and length(list.1$c)==3, I would like to use for randomising list.1$b values from list.1$a and list.1$c, and for randomising list.1$c values from list.1$b and list.1$d. Any ideas how to achieve this??
Many thanks in advance 
UPDATE
A solution provide for @dardisco:
l2 <- list()
ll1 <- length(list.1)
length(l2) <- ll1
set.seed(4)
for (i in 1:ll1){
   vec1 <- list.1[[i]]
    jl <- 1;jr<-1; #i've added two counters one for left one for right
    while (length(vec1) < 4){
       if(i==1) {
          vec1 <- c(vec1, list.1[[i+jr]])
          jr <- jr+1
        } else if (i==ll1 || jr+i==ll1 ){ #to avoid out of boundaries, so many elements with less than 4 elements close to the end
           vec1 <- c(vec1, list.1[[i-jl]])
           jl <- jl+1
    }else {
           vec1 <- c(vec1, list.1[[i-jl]], list.1[[i+jr]])
               jl <- jl+1
        jr <- jr+1
        } 
 } 
   l2[[i]] <- sample2(vec1, 10, replace=TRUE)   
}

A:

Following on from @DWins advice - is this what you're looking for? 
l2 <- list()
length(l2) <- length(list.1)
set.seed(1)
for (i in 1:length(list.1)){
    if ( length(list.1[[i]]) >=4 ){
        l2[[i]] <- sample(list.1[[i]], 10, replace=TRUE)
        } else {
            l2[[i]] <- sample(c(list.1[[i]],list.1[[i-1]],list.1[[i+1]]),
                                10, replace=TRUE)
            }
    }

Note that this assumes the first and last elements in the list have >=4 elements.
Update
In light of your comment - start with an example which better illustrates the problem:
list.1 <- list(a=letters[1:2],
               b=letters[3],
               c=letters[10:14],
               d=letters[25:26])

then
l2 <- list()
ll1 <- length(list.1)
### ll1 = length of list.1
length(l2) <- ll1
set.seed(4)
for (i in 1:ll1){
### vec1 = default vector from which to sample
    vec1 <- list.1[[i]]
### j = counter for position relative to current
    j <- 1
### if sample size <4 (the threshold) then include additional elements in list until >=4
### change this to 50 if required, as in:
### while (length(vec1) <50){
    while (length(vec1) <4){
### check if at first element
        if(i==1) {
### keep adding successive elements from list.1 to end of vec1
            vec1 <- c(vec1, list.1[[i+j]])
            j <- j+1
### if at last element, add preceding elements
        } else if (i==ll1 ){
            vec1 <- c(vec1, list.1[[i-j]])
            j <- j+1
        } else {
### you could add both in one step, like so:
### vec1 <- c(vec1, list.1[[i-j]], list.1[[i+j]])
### j <- j+1
### }
### or do it in two steps as below:
###
### k = counter to indicate moving forward or back 
            k <- 1
### if odd, add next element
            if (!k %% 2==0){
                vec1 <- c(vec1, list.1[[i+j]])
            } else {
### if even, add preceding element and increment counter for relative position
                vec1 <- c(vec1, list.1[[i-j]])
                j <- j+1
            }
            k <- k+1
        }
    }
    l2[[i]] <- sample(vec1, 10, replace=TRUE)
}

This should do what you want, although there may be a prettier way. Gains from vectorisation are likely to be modest at best.

