# R Question - How to generate the 50 first odd numbers and store them as a vector?

#### econlearner

##### New Member
Hello,

This might sound really basic as I am completely new to R. Any help is appreciated.

#### Jake

There are many ways to do this. The first and most straightforward that jumps to mind is to use the seq command, as in
Code:
seq(1, 99, 2)
You could also get the same result without having to know the last value by doing something like
Code:
(1:50)*2 - 1
And of course there are other ways.

#### Lazar

##### Phineas Packard
A third way for fun:
Code:
x <- c(1:100)[c(T,F)]

#### bryangoodrich

##### Probably A Mammal
or

Code:
seq(1, by = 2, len = 50)
I think that's more directly translating what he's asking for. You all assume to already know the start and end points of this sequence, etc. This uses just what information is required: "starting from one, going every other one, for 50 values."

#### derksheng

##### New Member
Another unnecessary alternative:

Code:
seq(50)*2-1

seq(50/2)*2-1
Fits nicely conceptually with $$\{2k+1 : k \in \mathbb{Z}\}$$

Last edited:

#### trinker

##### ggplot2orBust
Another way using the modulas (I think it's called that or a similar name); %%

Code:
x <- 50; c(1:x*2)[c(1:x*2)%%2 < 1]

#### Dason

Another way using the modulas (I think it's called that or a similar name); %%

Code:
x <- 50; c(1:x*2)[c(1:x*2)%%2 < 1]
I love that you go all complicated and then accidentally generate the even numbers instead of the odds. Note that 1:x*2 is the same as (1:x)*2 since : has higher precedence than *. Also %% is modulo so if we're doing x %% 2 then if we want to find odd numbers we want this to be equal to 1 - whereas you're only grabbing the ones that are less than 1.

#### trinker

##### ggplot2orBust
I actually wrote it at church from my phone so couldn't actually check it

Code:
x <- 50; c(1:(x*2))[c(1:(x*2))%%2 != 0]
Take 2 works

#### derksheng

##### New Member
Really bored

Code:
do.call(rbind,lapply(1:98, function(i) match(c(letters[seq(99)*2][!is.na(letters[seq(99)*2])],toupper(letters[seq(99)*2][!is.na(letters[seq(99)*2])])),c("Lol",letters,LETTERS))))[sample(50)[1],which(match(c(letters[seq(99)*2][!is.na(letters[seq(99)*2])],toupper(letters[seq(99)*2][!is.na(letters[seq(99)*2])])),c("Lol",letters,LETTERS))<51)]

#### Dason

Well let me allow you work on this for even longer by pointing out that 1 is the first odd number (which you missed) and the goal was to generate the first 50 odd numbers - not the odd numbers less than 50. Go!

#### Dason

Also I'll throw my own (clearly superior) way of doing this into the mix.

Code:
out <- c(1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31,
33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63,
65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95,
97, 99)

#### Dason

Triple post? Don't mind if I do.

Clearly this solution is the one that is most "R like" in spirit:

Code:
library(inline)
code <- "
int i;
x[0] = 1;
for(i = 1; i < n[0]; i++){
x[i] = x[i-1] + 2;
}

"

oddgen <- cfunction(signature(n = "integer", x = "integer"), code, convention = ".C")
myoddgen <- function(n){
x <- integer(n)
oddgen(n, x)$x } out <- myoddgen(50) which outputs Code: > out [1] 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 [24] 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 [47] 93 95 97 99 #### trinker ##### ggplot2orBust Alright if it's a waste you're time fest I'm up next: Code: is.Integer <- function(x, tol = .Machine$double.eps^0.5)  abs(x - round(x)) < tol
x <- 50; z <- c(1:(x*2)); z[!is.Integer(z/2)]