[R] for ( i in 23:0 ) { V <- vector [ 1 : i ] }

Rui Barradas ruipbarradas at sapo.pt
Wed Oct 24 18:46:26 CEST 2012


Hello,

Try the following.

lapply(1:23, function(i) 1:i)

As for why yuor code doesn't work, you're assigning 24 times a value to 
V, on loop exit only the last one is there. A correct way using loops 
would be


V <- list()
for(i in 1:23)
     V[[i]] <- 1:i


Beware that the last vector goes from 1 _down_ to 0.

And do read An Introduction to R, file R-intro.pdf in your R/doc directory.

Hope this helps,
Em 24-10-2012 13:25, colaiutachambers escreveu:
> I'm used to run that on Matlab
>>> for indice=1:23,
> V=vector(1:indice)
> end
> that give me 23 vectors as output.
>
> Why the same command on R  " for ( i in 23:0 ) 
{ V <- vector [ 1 : i ] } "
> returns one only vector?
> How can I obtain the 23 vectors that I need ?
>
> Thank you.
>
>
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/for-i-in-23-0-V-vector-1-i-tp4647306.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.




More information about the R-help mailing list