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

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

Hello,

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

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

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.

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

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

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

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

5. ## The Following 2 Users Say Thank You to bryangoodrich For This Useful Post:

Dason (09-16-2012), econlearner (09-16-2012)

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

Another unnecessary alternative:

Code:
seq(50)*2-1

seq(50/2)*2-1
Fits nicely conceptually with

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

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]

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

Thank you all for the help.

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

Originally Posted by trinker
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.

10. ## The Following User Says Thank You to Dason For This Useful Post:

trinker (09-16-2012)

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

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

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

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)]

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

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!

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

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)

15. ## The Following 3 Users Say Thank You to Dason For This Useful Post:

derksheng (09-16-2012), GretaGarbo (09-17-2012), trinker (09-16-2012)

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

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 17. ## Re: R Question - How to generate the 50 first odd numbers and store them as a vector? 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)]

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

Originally Posted by trinker
Alright if it's a waste you're time fest I'm up next
What?!? Clearly coding a C function that just creates a specialized sequence is totally a justified way to do this and clearly not just a waste of time... clearly.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts