1

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)

Flo Chi
  • 65
  • 2
  • 9
  • What is the structure of `trendtest`? A list of lists of length `list` where every element consists of `liste`? – erasmortg Jul 30 '15 at 14:31
  • No. "trendtest" is a list and contains all different vectors "liste". It has exactly the same amount of entries as "list" as each list[[u]] creates a certain vector "liste". – Flo Chi Jul 30 '15 at 14:46
  • Can't you initialize trendtest before your for loop `trendtest <- list()` and then do `trendtest[[u]] <- liste` after the `while`, in your code, your `lapply` will just return a list where each element is the last `liste`. – NicE Jul 30 '15 at 14:49
  • I tried this before. It gives me the list "trendtest" with those 2000 entries, BUT all entries exept for the 2000th are "NULL". Just the last one is a vector ("liste") – Flo Chi Jul 30 '15 at 15:01
  • Did you try this instead? `trendtest <- lapply(1:length(list), function(u) u<-liste)`, that creates a list of length(list) (in my mockup data) with the elements of liste, but perhaps it's twice the size? – erasmortg Jul 30 '15 at 15:44
  • Ijust tried it but it gives me always the same vector in "trendtest" – Flo Chi Jul 30 '15 at 20:34
  • @FloChi please edit your question with a MRE as suggested in this question http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example i have a feeling it's got to do with indexing, but it's hard to say at this point – erasmortg Jul 31 '15 at 10:56
  • howaboutsomespacesinyourcode? – Señor O Jul 31 '15 at 18:28

1 Answers1

2

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.

CL.
  • 14,577
  • 5
  • 46
  • 73
  • @ user2706569: Sorry I'm not such a pro :D I don't really understand this. Is it possible that I send you the code in a .txt file via PM and you have a short look? You'll see the problem in a second ... after all I think it's just some small issue – Flo Chi Jul 30 '15 at 20:45
  • There are no PMs on Stack Overflow. But if this doesn't solve your question then you may a) ask questions on what you don't understand (here in the comments) or b) amend your question with a [minimal, complete, verifiable example](http://stackoverflow.com/help/mcve) (see also the [R specific version](http://stackoverflow.com/a/5963610/2706569)). – CL. Jul 30 '15 at 22:13
  • @FloChi I updated my answer in response the the edited question. – CL. Jul 31 '15 at 18:25