Re: [R] Storing output of loop into list()

2016-04-07 Thread Adams, Jean
You don't need a for loop for the first part.  You can do it like this:

Notice that I used unchanging data for the temps vector so that you could
tell us what output you expect to see, in case what I provide is not
adequate.

temps <- c(13.988, 13.932, 14.039, 14.082, 13.998, 13.93, 14.028, 14.015,
  14.06, 14.092, 14.089, 13.971, 14.062, 14.001, 13.959, 14.046,
  14.034, 14.089, 13.991, 13.982, 14.038, 14.001, 13.973, 13.966,
  13.995, 13.934, 14.101, 14.087, 14.064, 14.06, 14.027, 13.996,
  14.037, 14.024, 14.032, 14.052, 13.986, 14.021, 13.988, 13.98,
  13.968, 13.959, 14.055, 13.978, 14.105, 14.005, 13.996, 14.027,
  13.99, 13.966, 14.047, 13.903, 13.953, 14.02, 13.969, 14.051,
  14.027, 14.03, 14.078, 13.988, 14.007, 13.899, 14.023, 13.991,
  13.993, 13.973, 14.035, 14.091, 14.033, 13.943, 14.08, 14, 14.015,
  14.042, 13.993, 14.064, 14.039, 13.939, 13.95, 14.017, 13.984,
  14.075, 14.006, 14.029, 14.004, 13.974, 14.003, 14.073, 13.991,
  13.973, 14.029, 14.02, 14.032, 14.036, 14.021, 13.983, 13.981,
  13.977, 13.94, 14.014)

tempdif9 <- -diff(temps, lag=9)
L <- length(tempdif9)
sel <- tempdif9[-L] >= 0.1 & tempdif9[-1] > -0.05
ttind <- cbind(tempdif9, index=1:L)[sel, ]
ttind

 tempdif9 index
[1,]0.10110
[2,]0.10017
[3,]0.10128
[4,]0.15243

I'm not sure what you are trying to achieve with the second part.  I
suspect that what you really wanted is to see the temperatures that
contributed to the output in ttind.  If that's the case, then perhaps this
will help.

contrib <- t(sapply(ttind[, "index"], function(i) temps[i + 0:8]))
contrib

   [,1]   [,2]   [,3]   [,4]   [,5]   [,6]   [,7]   [,8]   [,9]
[1,] 14.092 14.089 13.971 14.062 14.001 13.959 14.046 14.034 14.089
[2,] 14.034 14.089 13.991 13.982 14.038 14.001 13.973 13.966 13.995
[3,] 14.087 14.064 14.060 14.027 13.996 14.037 14.024 14.032 14.052
[4,] 14.055 13.978 14.105 14.005 13.996 14.027 13.990 13.966 14.047

Jean


On Thu, Apr 7, 2016 at 11:09 AM,  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@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.
>

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] Storing output of loop into list()

2016-04-07 Thread Bert Gunter
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,   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@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.

__
R-help@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.