Flo Chi
Flo Chi

Reputation: 65

saving vectors from a loop in a list in r

Hi I never edited a question of mine but I'll give it a try. It's not soo extremely important what the code means actually. For me only saving the vectors "liste" in a new list is relevant :D

test <- list()
test <- replicate(5, sample(1:100, 50), simplify = FALSE)  # Creates a list of 5 vectors
> test[[1]]
[1]  90  96  20  86  32  77  83  33  64  29  88  97  78  81  40  60  89  19  31  59  26  38  34  71   5  80  85
[28]   3  70  87  41  50   6  18  37  58   9  76  91  62  12  30  42  94  72  95 100  10  68  82

S <- test[[1]]

x <- diff(S)   # following algorythm creates "liste" (vector) for test [[1]]
trendtest <- list()
k <- NULL                          
d <- NULL                                
t <- vector("list",length(x))
A <- vector("list",length(x)) 
z <- vector("list",length(x)-2)
za <- vector("list",length(x)-2)
liste <- NULL
dreisum <- sapply(1:(length(x)-2), function(i) sum(x[c(i,(i+1))]))
dreisumi <- lapply(1:(length(x)-2), function(i) dreisum[i:(length(x)-2)]) 
zdreisumi<- lapply(1:(length(x)-4), function(i) dreisumi[[i]]  [3:length(dreisumi[[i]])]<0)
zadreisumi<- lapply(1:(length(S)-4), function(i) dreisumi[[i]][3:length(dreisumi[[i]])]>0)
Si <- lapply(1:(length(x)-2), function(i) S[i:(length(x))])  
i <- 1
h <- 1
while(i<(length(x)-3) & h!=Inf){                   
k <- c(k,k <- (S[i]-S[i+2])/(-2))
d <- c(d,d <- (S[i+2]*i-S[i]*(i+2))/(-2))
t[[i]] <- i:(length(x))
A[[i]] <- k[length(liste)+1]*t[[i]]+d[length(liste)+1]
A[[i]][3] <- S[i+2]
z[[i]] <- Si[[i]][3:length(Si[[i]])]<A[[i]][3:length(A[[i]])]
za[[i]] <- Si[[i]][3:length(Si[[i]])]>A[[i]][3:length(A[[i]])]

if(k[length(liste)+1]>0 & S[i+3]>A[[i]][4] & is.element(TRUE,z[[i]])){h <- (min(which(z[[i]]!=FALSE))+1)}else{
     if(k[length(liste)+1]>0 & S[i+3]<A[[i]][4] & is.element(TRUE,za[[i]])){h <- (min(which(za[[i]]!=FALSE))+1)}else{
         if(k[length(liste)+1]<0 & S[i+3]>A[[i]][4] & is.element(TRUE,z[[i]])){h <- (min(which(z[[i]]!=FALSE))+1)}else{
            if(k[length(liste)+1]<0 & S[i+3]<A[[i]][4] & is.element(TRUE,za[[i]])){h <- (min(which(za[[i]]!=FALSE))+1)}else{
                if(k[length(liste)+1]>0 & S[i+3]>A[[i]][4] & (all(z[[i]]==FALSE))){h <- (min(which(zdreisumi[[i]]!=FALSE))+2)}else{
                    if(k[length(liste)+1]>0 & S[i+3]<A[[i]][4] & (all(za[[i]]==FALSE))){h <- (min(which(zdreisumi[[i]]!=FALSE))+2)}else{
                         if(k[length(liste)+1]<0 & S[i+3]>A[[i]][4] & (all(z[[i]]==FALSE))){h <- (min(which(zadreisumi[[i]]!=FALSE))+2)}else{
                             if(k[length(liste)+1]<0 & S[i+3]<A[[i]][4] & (all(za[[i]]==FALSE))){h <- (min(which(zadreisumi[[i]]!=FALSE))+2)}}}}}}}}
liste <- c(liste,i)
 i <- i+h-1   
 if((length(x)-3)<=i & i<=length(x)){liste <- c(liste,i)}}

 > liste
 [1]  1  3  7 10 12 16 18 20 24 27 30 33 36 39 41 46

Actually the whole code is not so interesting for my problem because it works! I made the example for test[[1]] now. BUT I want that a for-loop (or whatever) takes ALL vectors in "test" and saves ALL 5 vectors "liste" in a new list (lets call it "trendtest" ... whatever :D)

Upvotes: 1

Views: 2215

Answers (1)

CL.
CL.

Reputation: 14997

The following will do what you ask for:

  • Delete the line trendtest <- list().
  • Take the code from x <- diff(S) to last line (except the very last line that only prints liste) and insert it at the position indicated by the placeholder __CODE_HERE__.
trendtest <- lapply(test, FUN = function(S) {
  __CODE_HERE__
  return(liste)
})

This is the "R way" of doing what you want. Alternatively, you could do the following (which is closer to your initial approach, but less the "R way"):

trendtest <- vector("list", length(test))

for (u in 1:length(test)) { # better: u in seq_along(test)
    S <- test[[u]]

    __CODE_HERE__

    trendtest[[u]] <- liste
}

Note that there will be an error message which is due to the sample data (which doesn't fit the algorithm provided) and is unrelated to saving liste in trendtest.

Upvotes: 2

Related Questions