create nested for loop

Hi,

I am trying to write a for nested loop to fill a matrix with the following 227 sample

sample 1

np1=1000
all_yt_s1<- matrix(nrow = 229,
                   ncol = np1 )
for( i in 1:np1){
  
  all_yt_s1[,i]<-rowSums(all_wts1[i,] *log.return[[1]]) }

sample 2

all_yt_s2<- matrix(nrow = 229,
                  ncol = np1 )


for( i in 1:np1){
  
  all_yt_s2[,i]<-rowSums(all_wts1[i,] *log.return[[2]]) }

:
:
until sample 227

all_yt_s227<- matrix(nrow = 229,
                  ncol = np1 )

for( i in 1:np1){
  
  all_yts_227[,i]<-rowSums(all_wts1[i,] *log.return[[227]]) }

please can you help me to write a nested for loop and store all total sample results in list for instance

Thank you in advance

its a challenge to work on code without data ... but here is my best guess at what would work for you

do_thing <- function(common_in, lr, np1 = 1000, nr = 229) {
  res_m <- matrix(
    nrow = nr,
    ncol = np1
  )
  for (i in 1:np1) {
    res_m[, i] <- rowSums(common_in[i, ] * lr)
  }
  res_m
}

overall_results <- lapply(
  1:227,
  \(x)do_thing(
    common_in = all_wts1,
    lr = log.return[[x]]
  )
)
2 Likes

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.