There are many ways to get the output you want, so your question is ill 
defined. But it is easy to see where your code goes wrong. And it should be 
easy for you to fix it.

If you subset a vector with the '[' operator, this is like putting a vector of 
indices "into" the square brackets. So, to debug such code, just print out your 
indices. And understand how the bracketing and operator priorities work:

i <- 2; n <- 7
1:n+(i-1)*(n+1)   # 1:7 gets produced first, then (i-1)*(n+1) == (1)*(8) is 
added to that vector

[1]  9 10 11 12 13 14 15

But consider:
i <- 2; n <- 7
1:(n+(i-1)*(n+1))  # 7+(1)*(8)
[1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15

You can probably figure it out now.


B.



> On Aug 18, 2017, at 7:31 AM, Moohwan Kim <kmhl...@gmail.com> wrote:
> 
> Dear R users,
> 
> I have the following codes:
> zeta <- rep(1,8)
> n <- 7
> for (i in 1:2){
> beta <- zeta[1:n+(i-1)*(n+1)]
> print(beta)
> parm <- zeta[i*(n+1)]
> print(parm)
> }
> ###################
> The output is as follows:
> [1] 1 1 1 1 1 1 1
> [1] 1
> [1] NA NA NA NA NA NA NA
> [1] NA
> #######################
> The outcome I want to get is:
> [1] 1 1 1 1 1 1 1
> [1] 1
> [1] 1 1 1 1 1 1 1
> [1] 1
> 
> How could I get the desired outcome?
> 
> best,
> kmh
> 
>       [[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to