[R] Storing output of loop into list()

Bert Gunter bgunter.4567 at gmail.com
Thu Apr 7 18:17:53 CEST 2016


Have a look at ?lapply, which will do this for you.

Have you spent time with an R tutorial or two. There are many good
ones on the web that discuss this sort of thig.

Cheers,
Bert


Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Thu, Apr 7, 2016 at 9:09 AM,  <maettuw at students.unibe.ch> wrote:
> Hello. I am trying to store the output from a loop into a matrix. Failed so far, thanks for the help.
>
> What I want to store into a new matrix: (just run the code once):
>
> temps<-rnorm(400,14,0.05)
> ttind<-NULL
> for(ti in 1:(length(temps)-9)) {
>   if(temps[ti]-temps[ti+9] >= 0.1 && max(temps[ti]-temps[ti+1:9]) > -0.05)
>     ttind<-c(ttind,ti)
> }
> for(ti in 1:length(ttind)) {
>
>  print(round(temps[ttind[ti]:(ttind[ti]+9)],3))
>
>   }
>
>
>
>
>
>
>
> My (failed) soultion attempt:
>
> temps<-rnorm(400,14,0.05)
> ttind<-NULL
> for(ti in 1:(length(temps)-9)) {
>   if(temps[ti]-temps[ti+9] >= 0.1 && max(temps[ti]-temps[ti+1:9]) > -0.05)
>     ttind<-c(ttind,ti)
> }
>
> outputList = list()
> counter = 1
>
> for(ti in 1:length(ttind)) {
>
>   outputList[i] <-  print(round(temps[ttind[ti]:(ttind[ti]+9)],3))
> counter= counter+1
>   }
>
> print(outputList)
>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list