R - How can I calculate a matrix with a function? -


i have problem following code in r. want myfun() function write data my.res matrix. can print statement my.vec[i]/my.vec[i-j] easily.

my.vec <-c(1:10) my.res <-matrix( ,10,2)  myfun <-function(j=2){    for(i in (j+1):10){      my.res[i,1] <-my.vec[i]/my.vec[i-j]      print(my.vec[i]/my.vec[i-j])    } } 

you have print in wrong location, changed input j=2 j can assigned. mentioned - don't need loop - seems trying learn how work loops?

myfun <- function(j){   for(i in (j + 1):10){     my.res[i,1] <- my.vec[i]/my.vec[i-j]   }   print(my.res) }  myfun(2) 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -