+ Reply to Thread
Results 1 to 15 of 15

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

  1. #1
    Points: 301, Level: 6
    Level completed: 2%, Points required for next Level: 49

    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    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. #2
    Cookie Scientist
    Points: 6,080, Level: 50
    Level completed: 65%, Points required for next Level: 70
    Jake's Avatar
    Location
    Boulder, CO
    Posts
    814
    Thanks
    18
    Thanked 320 Times in 245 Posts

    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.
    “In God we trust. All others must bring data.”
    ~W. Edwards Deming

  3. #3
    TS Contributor
    Points: 6,701, Level: 53
    Level completed: 76%, Points required for next Level: 49
    Lazar's Avatar
    Location
    Sydney
    Posts
    675
    Thanks
    111
    Thanked 167 Times in 152 Posts

    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. #4
    Probably A Mammal
    Points: 14,712, Level: 78
    Level completed: 66%, Points required for next Level: 138
    bryangoodrich's Avatar
    Location
    Sacramento, California, United States
    Posts
    1,963
    Thanks
    223
    Thanked 422 Times in 389 Posts

    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. #5
    Points: 1,980, Level: 26
    Level completed: 80%, Points required for next Level: 20
    derksheng's Avatar
    Posts
    247
    Thanks
    49
    Thanked 34 Times in 28 Posts

    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 \{2k+1 : k \in \mathbb{Z}\}
    Last edited by derksheng; 09-16-2012 at 09:52 AM.

  7. #6
    FormerlyKnownAsRaptor
    Points: 25,003, Level: 95
    Level completed: 66%, Points required for next Level: 347
    trinker's Avatar
    Location
    Buffalo, NY
    Posts
    3,218
    Thanks
    919
    Thanked 562 Times in 509 Posts

    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]
    "If you torture the data long enough it will eventually confess."
    -Ronald Harry Coase -

  8. #7
    Points: 301, Level: 6
    Level completed: 2%, Points required for next Level: 49

    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

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

    Thank you all for the help.

  9. #8
    RotParaTon
    Points: 47,119, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Discussion EnderPosting AwardCommunity AwardMaster TaggerFrequent Poster
    Dason's Avatar
    Location
    Ames, IA
    Posts
    9,185
    Thanks
    212
    Thanked 1,640 Times in 1,401 Posts

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

    Quote Originally Posted by trinker View Post
    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.
    "His programming is malfunctioning. It begins! Get your weapons, he's going to become a killbot!!!" - bryangoodrich

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

    trinker (09-16-2012)

  11. #9
    FormerlyKnownAsRaptor
    Points: 25,003, Level: 95
    Level completed: 66%, Points required for next Level: 347
    trinker's Avatar
    Location
    Buffalo, NY
    Posts
    3,218
    Thanks
    919
    Thanked 562 Times in 509 Posts

    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
    "If you torture the data long enough it will eventually confess."
    -Ronald Harry Coase -

  12. #10
    Points: 1,980, Level: 26
    Level completed: 80%, Points required for next Level: 20
    derksheng's Avatar
    Posts
    247
    Thanks
    49
    Thanked 34 Times in 28 Posts

    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. #11
    RotParaTon
    Points: 47,119, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Discussion EnderPosting AwardCommunity AwardMaster TaggerFrequent Poster
    Dason's Avatar
    Location
    Ames, IA
    Posts
    9,185
    Thanks
    212
    Thanked 1,640 Times in 1,401 Posts

    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!
    "His programming is malfunctioning. It begins! Get your weapons, he's going to become a killbot!!!" - bryangoodrich

  14. #12
    RotParaTon
    Points: 47,119, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Discussion EnderPosting AwardCommunity AwardMaster TaggerFrequent Poster
    Dason's Avatar
    Location
    Ames, IA
    Posts
    9,185
    Thanks
    212
    Thanked 1,640 Times in 1,401 Posts

    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)
    "His programming is malfunctioning. It begins! Get your weapons, he's going to become a killbot!!!" - bryangoodrich

  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. #13
    RotParaTon
    Points: 47,119, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Discussion EnderPosting AwardCommunity AwardMaster TaggerFrequent Poster
    Dason's Avatar
    Location
    Ames, IA
    Posts
    9,185
    Thanks
    212
    Thanked 1,640 Times in 1,401 Posts

    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
    "His programming is malfunctioning. It begins! Get your weapons, he's going to become a killbot!!!" - bryangoodrich

  17. #14
    FormerlyKnownAsRaptor
    Points: 25,003, Level: 95
    Level completed: 66%, Points required for next Level: 347
    trinker's Avatar
    Location
    Buffalo, NY
    Posts
    3,218
    Thanks
    919
    Thanked 562 Times in 509 Posts

    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)]
    "If you torture the data long enough it will eventually confess."
    -Ronald Harry Coase -

  18. #15
    RotParaTon
    Points: 47,119, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Discussion EnderPosting AwardCommunity AwardMaster TaggerFrequent Poster
    Dason's Avatar
    Location
    Ames, IA
    Posts
    9,185
    Thanks
    212
    Thanked 1,640 Times in 1,401 Posts

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


    Quote Originally Posted by trinker View Post
    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.
    "His programming is malfunctioning. It begins! Get your weapons, he's going to become a killbot!!!" - bryangoodrich

+ Reply to Thread

Posting Permissions

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








Advertise on Talk Stats