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

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

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

colaiutachambers
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.

Reply | Threaded
Open this post in threaded view
|

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

Bert Gunter
Quoting Rolf Turner:

(1) Learn something about R; don't just hammer and hope.  Read the
introductory manuals and scan the FAQ.

-- Bert

On Wed, Oct 24, 2012 at 5:25 AM, colaiutachambers
<[hidden email]> wrote:

> 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.
>
> ______________________________________________
> [hidden email] 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.



--

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

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

Rui Barradas
In reply to this post by colaiutachambers
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.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

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

colaiutachambers
Thank you very much Rui.
I was in hurry because I'm working in a bank and on my master's degree thesys at the same time.
I know Matlab, but they want I use R to price a convertible bond (I can't get why...).
I read "An introduction to R" the last week-end and now I feel more confortable.
This is an help section and was not looking for a good telling-off but I'm sorry if I bothered somoone with my question.