如何使用for循环生成一个向量

时间:2022-09-25 04:17:58

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

我对R(以及一般的编程)非常陌生,我已经被这个(可能非常简单的)问题困扰了好几天……

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

如何使向量3 6 12 24 48 96 192 384 768带有一个for循环?

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!

如果有人能给我指出正确的方向,那将是一个很大的帮助!

3 个解决方案

#1


14  

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

将使向量x包含(1,2,3)就像你做了x <-(1,2,3)一样。接下来你需要意识到的是目标向量的每个元素都是之前的两倍,这在for循环中很容易做到

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.

将会有n倍的每个循环。显然,您可以将其他语句放在n <- n*2语句之前或之后,来使用它的双精度或非双精度形式。

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

希望你能把这两件事放在一起来做你想要的循环。

#2


14  

x=c()
x[1] = 3
for (i in 2:9) { 
    x[i]=2*x[i-1]
}

#3


2  

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

真的,人。坚持Arun评论中隐藏的解决方案。

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

#1


14  

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

将使向量x包含(1,2,3)就像你做了x <-(1,2,3)一样。接下来你需要意识到的是目标向量的每个元素都是之前的两倍,这在for循环中很容易做到

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.

将会有n倍的每个循环。显然,您可以将其他语句放在n <- n*2语句之前或之后,来使用它的双精度或非双精度形式。

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

希望你能把这两件事放在一起来做你想要的循环。

#2


14  

x=c()
x[1] = 3
for (i in 2:9) { 
    x[i]=2*x[i-1]
}

#3


2  

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

真的,人。坚持Arun评论中隐藏的解决方案。

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