Skip to content

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

4 messages · Bert Gunter, Rui Barradas, colaiutachambers

#
I'm used to run that on Matlab
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.
#
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
<gabriele.carrarini at gmail.com> wrote:

  
    
#
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:
{ V <- vector [ 1 : i ] } "
4 days later
#
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.





--
View this message in context: http://r.789695.n4.nabble.com/for-i-in-23-0-V-vector-1-i-tp4647306p4647745.html
Sent from the R help mailing list archive at Nabble.com.