Reputation: 21
In R, I need to create a vector b = (1, 1+pi, 1+2pi, 1+3pi,...,1+19pi)
. I am unsure how to do this. I keep trying to use the seq
command (i.e. seq(1, 1+npi n = 1:19)
and that's totally wrong!), but don't know the proper syntax to make it work, thus it never does.
Any help would be appreciated.
Upvotes: 1
Views: 41
Reputation: 48251
You should use simply 0:19 * pi + 1
. Using seq
is not so nice: seq(1, 1 + 19 * pi, by = pi)
or seq(1, 1 + 19 * pi, length = 20)
.
Upvotes: 1
Reputation: 263481
R needs the multiplication operator.
b <- 1+ seq(0,19)*pi
Or slightly faster in situations where speed might matter:
b <- 1+ seq.int(0,19)*pi
You could use the equivalent:
b <- 1+ 0:19*pi
Because the ":" operator has very high precedence ( see ?Syntax
), it's reasonable safe. Just be careful that you understand precedence when you use a minus or plus sign where it might be parse as a binary operator (remembering that spaces are ignored and that unary-minus has higher precedence than the single-colon, but binary minus or plus has a lower precedence :
> 1: 5+5
[1] 6 7 8 9 10
Upvotes: 2