Construind a For using the R

Asked

Viewed 49 times

0

Hello, I think my question is very simple, but come on:

How do I make such a move:

coef.matrix=matrix(0,4,16)

for (k in c(1,4)){
  for(i in 1:2){
    coef.matrix[i,1]=summary(lm(y ~ x))$coef[2,k]
  }

}

Instead of "1 to 4" I would like to include "1 or 4". You can do this within parentheses or I will need to call the if / else?

  • 1

    you are wanting to make a for or a if?

  • 2

    If it’s 1 and 4, you use it, like: for (i in c(1,4)){ expression}, but if it’s 1 or 4, you’ll probably use if.

  • Guys, thanks for editing the code. @Danielfalbel

1 answer

0


Do it like this:

for (k in c(1,4)) {
}
  • thanks. I edited the question. See if it helps.

  • in his suggestion he begins with k=1 and concludes the first part of the for. Then it goes to k=4 and concludes the second part of the 4? It seems that in my case they only consider k=4. Very strange

  • 1

    In this example you will pass in each of the elements of the list, which are 1 and 4. Try running : for (k in c(1,4)) print(k);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.