[R] Looping help

Jim Lemon drjimlemon at gmail.com
Fri Jul 31 13:23:13 CEST 2015


Hi April,
You need nested loops for something like this

qs<- c(0,0.25,0.5,1,2,4,8,16,32,64)
nrows<-dim(Data)[1]
nqs<-length(qs)
D.mat<-SE.mat<-matrix(NA,nrow=nrows,ncol=nqs)
for(row in 1:nrows) {
 for(qval in 1:nqs) {
  # perform your calculation and set D.mat[row,qval] and
SE.mat[row,qval] to the return values
 }
}

Jim

> From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of April
> Smith
> Sent: Friday, July 31, 2015 2:21 AM
> To: r-help at r-project.org
> Subject: [R] Looping help
>
> I have never looped before and know I need to.  I am unsure how to
> proceed:
>
>
>    - Action I need done: d(Data[1,2:399], q=0, boot=TRUE,
>    boot.arg=list(num.iter=1000))
>    - I need this to happen to all rows, I need All[1,2:399] to increase
> to
>    All[2:399], etc.
>    - But I also need the results from q increasing from 0 to 0.25, 0.5,
> 1,
>    2, 4,8,16,32,64 before the loop moves on to the next row.
>    - For each iteration I will receive two values: D and st.err.  I
> need
>    this put into a matrix
>
>
> I feel like this should be pretty simple to learn, but I have never
> looped before.
>
> I am hoping to get more of a tutorial on how to write loop code, then
> to just be given the loop code.
>
> Thanks,
> April
>
>       [[alternative HTML version deleted]]



More information about the R-help mailing list