21

I'm very new to R (and programming in general) and I've been stuck on this (probably very easy) question for a few days...

How would one make the vector 3 6 12 24 48 96 192 384 768 with a for loop?

All I've managed to come up with so far is something along the lines of:

x=numeric()
for (i in 1:8) (x=2*i[-1])

However, that doesn't work. I think one of the main problems is that I don't understand how to index numbers in a sequence.

If anyone could point me in the right direction that would be such a great help!

d-cubed
  • 1,034
  • 5
  • 30
  • 58
user1990538
  • 227
  • 1
  • 2
  • 3
  • have you looked at http://stackoverflow.com/questions/8197554/r-create-a-vector-with-loop-structure ? – Rachel Gallen Jan 18 '13 at 13:28
  • 2
    Starting off @Arun's suggestion: better to start with `x <- rep(NA,9); x[1] <- 3`, then do the `for` loop. It is better to preallocate a vector of the correct length than to append a new element every time, since R will need to do a lot of internal rearranging and memory allocating if you append. (This of course only makes a difference for larger vectors, but try it both ways with a vector of length 1,000,000.) – Stephan Kolassa Jan 18 '13 at 13:33
  • 2
    @Arun: Sure understand. And I guess you mean `x <- 3*2^(0:8)`, right? ;-) – Stephan Kolassa Jan 18 '13 at 13:37

5 Answers5

20

Okay, the first thing you need to know is how to append things to a vector. Easily enough the function you want is append:

x <- c(1, 2)
x <- append(x, 3)

will make the vector x contain (1, 2, 3) just as if you'd done x <- (1, 2, 3). The next thing you need to realise is that each member of your target vector is double the one before, this is easy to do in a for loop

n <- 1
for (i in 1:8)
{
    n <- n*2
}

will have n double up each loop. Obviously you can use it in its doubled, or not-yet-doubled form by placing your other statements before or after the n <- n*2 statement.

Hopefully you can put these two things together to make the loop you want.

Jack Aidley
  • 19,439
  • 7
  • 43
  • 70
  • Brilliant, all these comments are really helpful! Thanks ever so much. – user1990538 Jan 18 '13 at 14:27
  • `append` is a handy func, but this is an awfully awkward way to go about building a very simple vector! – Carl Witthoft Jan 18 '13 at 15:42
  • 3
    I entirely agree, Carl, however from the structure of his post I presume that this is a learning exercise aimed at teaching loops, etc. rather than trying to figure out the best way to create a vector. – Jack Aidley Jan 18 '13 at 15:45
20
x=c()
x[1] = 3
for (i in 2:9) { 
    x[i]=2*x[i-1]
}
Tomas
  • 57,621
  • 49
  • 238
  • 373
5

Really, folks. Stick with the solution hiding in Arun's comment.

Rgames> 3*2^(0:20)
 [1]       3       6      12      24      48      96     192     384     768
[10]    1536    3072    6144   12288   24576   49152   98304  196608  393216
[19]  786432 1572864 3145728
Carl Witthoft
  • 20,573
  • 9
  • 43
  • 73
  • 5
    While I fully agree that this is a much better way to create the vector in question, it does not answer the question. Which, in view of it's structure I'm presuming to be an exercise intended to teach the use of the constructs rather than guide how to create a vector in general. – Jack Aidley Jan 18 '13 at 15:44
  • 1
    @JackAidley that could be, but frankly the days of loops are numbered :-). Any teacher who teaches loops before teaching about vectorization is not doing the class a favor. But more to the point, I've found both on SO and in meatspace that people are very bad at asking the actual question they need answered. This might be a homework assignment, but could equally well be a coding newb who could use some hints as to efficient ways to solve the real problem. – Carl Witthoft Jan 18 '13 at 15:50
  • This is an example showing that processing a vector is an implicit for. – kd4ttc Dec 30 '19 at 23:04
1

sapply(1:9, function(i) 3*2**(i-1) )

kd4ttc
  • 1,075
  • 1
  • 10
  • 28
0

You could, if you know python use the package reticulate to do it which would be much easier than in r (which is how I did a similar task).

If you don't have the package then do:

install.packages('reticulate')

Then if you do, you could do

library('reticulate')

py_run_string('vector = [n*2 for n in range(1, 9, 1)']
#Then you can call the vector by:

print(py$vector)

#And do whatever you want with it.  

I know that this was more python than r and it was made a bit more complicated then it could have been but for me personally, I found python more easier than r for manipulating vectors (or lists, the python equivalent). I hope that this answers your question.