Skip to content

for loop in R

3 messages · Jennifer Sheng, rsherry8, jennifer.sheng2002 at gmail.com

#
Dear friends,  I am working on a double loop using for.  One level of loop
is to predict N times for each subject, and the second level is to predict
M times for the every subject, one subject after one subject.   Please note
every subject have different N or M rows of data.   Any advice?  Thank you
so much!

Below is the current code:

set.seed (123)   ## for consistent result;

ND <- S004Cmin[S004Cmin$ID %in% c(1:10),]   # define the first 10 subjects

predSurv <- vector("list", nrow(ND))

for (i in 1:nrow(ND)) {

  set.seed(123)

  predSurv[[i]] <- survfitJM(fitJOINT.NULL, newdata = ND[1:i, ],
idVar="USUBJID")

  }



Thank you very much!

Jenny
#
I only see one for loop in your code. I am wondering if you want a 
second for loop based upon the length of newdata.

I would also think that you do not need the second call to set.seed.

Bob
On 1/12/2017 4:44 PM, Jennifer Sheng wrote:
#
That is right, Bob.  Only one loop for now, since I do not know how to set up the 2nd loop.  Any advice from the community?  Thank you!

Sent from my iPhone