Beginner problem - using mod function to print odd numbers

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
17 messages Options
Reply | Threaded
Open this post in threaded view
|

Beginner problem - using mod function to print odd numbers

nelpar

I don't understand. --

7%%2=1
9%%2=1
11%%2=1

What aren't these numbers printing ?


num<-0
for (i in 1:100){
  num<-num+i
if (num%%2 != 0)
  print(num)
}


[1] 1
[1] 3
[1] 15
[1] 21
[1] 45
[1] 55
[1] 91
[1] 105
[1] 153
[1] 171
[1] 231
[1] 253
[1] 325
[1] 351
[1] 435
[1] 465
[1] 561
[1] 595
[1] 703
[1] 741
[1] 861
[1] 903
[1] 1035
[1] 1081
[1] 1225
[1] 1275
[1] 1431
[1] 1485
[1] 1653
[1] 1711
[1] 1891
[1] 1953
[1] 2145
[1] 2211
[1] 2415
[1] 2485
[1] 2701
[1] 2775
[1] 3003
[1] 3081
[1] 3321
[1] 3403
[1] 3655
[1] 3741
[1] 4005
[1] 4095
[1] 4371
[1] 4465
[1] 4753
[1] 4851



--
Sent from: https://r.789695.n4.nabble.com/R-help-f789696.html

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Beginner problem - using mod function to print odd numbers

Hasan Diwan-2
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: Beginner problem - using mod function to print odd numbers

Eric Berger
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: Beginner problem - using mod function to print odd numbers

Jeff Newmiller
In reply to this post by nelpar
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: Beginner problem - using mod function to print odd numbers

Stefan Evert-3
In reply to this post by nelpar
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: Beginner problem - using mod function to print odd numbers

Rui Barradas
In reply to this post by nelpar
Hello,

Why not write a function?


odd <- function(x, numeric = TRUE){
   i <- x %% 2 == 1
   if(numeric) x[i] else i
}

odd(1:100)


Hope this helps,

Rui Barradas

Às 19:17 de 02/06/21, nelpar escreveu:

>
> I don't understand. --
>
> 7%%2=1
> 9%%2=1
> 11%%2=1
>
> What aren't these numbers printing ?
>
>
> num<-0
> for (i in 1:100){
>    num<-num+i
> if (num%%2 != 0)
>    print(num)
> }
>
>
> [1] 1
> [1] 3
> [1] 15
> [1] 21
> [1] 45
> [1] 55
> [1] 91
> [1] 105
> [1] 153
> [1] 171
> [1] 231
> [1] 253
> [1] 325
> [1] 351
> [1] 435
> [1] 465
> [1] 561
> [1] 595
> [1] 703
> [1] 741
> [1] 861
> [1] 903
> [1] 1035
> [1] 1081
> [1] 1225
> [1] 1275
> [1] 1431
> [1] 1485
> [1] 1653
> [1] 1711
> [1] 1891
> [1] 1953
> [1] 2145
> [1] 2211
> [1] 2415
> [1] 2485
> [1] 2701
> [1] 2775
> [1] 3003
> [1] 3081
> [1] 3321
> [1] 3403
> [1] 3655
> [1] 3741
> [1] 4005
> [1] 4095
> [1] 4371
> [1] 4465
> [1] 4753
> [1] 4851
>
>
>
> --
> Sent from: https://r.789695.n4.nabble.com/R-help-f789696.html
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Beginner problem - using mod function to print odd numbers

R help mailing list-2
In reply to this post by Stefan Evert-3
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: Beginner problem - using mod function to print odd numbers

Bert Gunter-2
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: Beginner problem - using mod function to print odd numbers

R help mailing list-2
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: Beginner problem - using mod function to print odd numbers

David Carlson
In reply to this post by R help mailing list-2
There is really no need for a loop:

num <- 1:100
num[ifelse(num %% 2 == 1, TRUE, FALSE)]

 [1]  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
[26] 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

David L Carlson


On Sat, Jun 5, 2021 at 2:05 PM William Michels via R-help
<[hidden email]> wrote:

>
> > i <- 1L; span <- 1:100; result <- NA;
> > for (i in span){
> + ifelse(i %% 2 != 0, result[i] <- TRUE, result[i] <- FALSE)
> + }
> > span[result]
>  [1]  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
> [30] 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99
> >
>
> HTH, Bill.
>
> W. Michels, Ph.D.
>
>
> On Sat, Jun 5, 2021 at 12:55 AM Stefan Evert <[hidden email]> wrote:
> >
> > >
> > > I don't understand. --
> > >
> > > 7%%2=1
> > > 9%%2=1
> > > 11%%2=1
> > >
> > > What aren't these numbers printing ?
> > >
> > > num<-0
> > > for (i in 1:100){
> > >  num<-num+i
> > > if (num%%2 != 0)
> > >  print(num)
> > > }
> >
> > Your code tests the numbers
> >
> >         1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, …
> >
> > and correctly prints the odd ones among them.
> >
> > But I suppose that's not what you wanted to do?
> >
> > ______________________________________________
> > [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> > https://urldefense.com/v3/__https://stat.ethz.ch/mailman/listinfo/r-help__;!!KwNVnqRv!Qe700OYnXI-Qe4XYE55MkVqMX3g4Ic4YFx87U-moxnT0xKw7m-rbNqU-EZd5ixk$
> > PLEASE do read the posting guide https://urldefense.com/v3/__http://www.R-project.org/posting-guide.html__;!!KwNVnqRv!Qe700OYnXI-Qe4XYE55MkVqMX3g4Ic4YFx87U-moxnT0xKw7m-rbNqU-fWVQ0xI$
> > and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://urldefense.com/v3/__https://stat.ethz.ch/mailman/listinfo/r-help__;!!KwNVnqRv!Qe700OYnXI-Qe4XYE55MkVqMX3g4Ic4YFx87U-moxnT0xKw7m-rbNqU-EZd5ixk$
> PLEASE do read the posting guide https://urldefense.com/v3/__http://www.R-project.org/posting-guide.html__;!!KwNVnqRv!Qe700OYnXI-Qe4XYE55MkVqMX3g4Ic4YFx87U-moxnT0xKw7m-rbNqU-fWVQ0xI$
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Beginner problem - using mod function to print odd numbers

David Carlson
If the loop is necessary:

num <- vector()
for (i in 1:100) {
    if(i %% 2 != 0) num <- c(num, i)
}
num

Or modify your code to this to get each odd number printed on a separate row:

for (i in 1:100) {
    if(i %% 2 != 0) print(i)
}

David L Carlson

On Sun, Jun 6, 2021 at 3:21 PM David Carlson <[hidden email]> wrote:

>
> There is really no need for a loop:
>
> num <- 1:100
> num[ifelse(num %% 2 == 1, TRUE, FALSE)]
>
>  [1]  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
> [26] 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
>
> David L Carlson
>
>
> On Sat, Jun 5, 2021 at 2:05 PM William Michels via R-help
> <[hidden email]> wrote:
> >
> > > i <- 1L; span <- 1:100; result <- NA;
> > > for (i in span){
> > + ifelse(i %% 2 != 0, result[i] <- TRUE, result[i] <- FALSE)
> > + }
> > > span[result]
> >  [1]  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
> > [30] 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99
> > >
> >
> > HTH, Bill.
> >
> > W. Michels, Ph.D.
> >
> >
> > On Sat, Jun 5, 2021 at 12:55 AM Stefan Evert <[hidden email]> wrote:
> > >
> > > >
> > > > I don't understand. --
> > > >
> > > > 7%%2=1
> > > > 9%%2=1
> > > > 11%%2=1
> > > >
> > > > What aren't these numbers printing ?
> > > >
> > > > num<-0
> > > > for (i in 1:100){
> > > >  num<-num+i
> > > > if (num%%2 != 0)
> > > >  print(num)
> > > > }
> > >
> > > Your code tests the numbers
> > >
> > >         1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, …
> > >
> > > and correctly prints the odd ones among them.
> > >
> > > But I suppose that's not what you wanted to do?
> > >
> > > ______________________________________________
> > > [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> > > https://urldefense.com/v3/__https://stat.ethz.ch/mailman/listinfo/r-help__;!!KwNVnqRv!Qe700OYnXI-Qe4XYE55MkVqMX3g4Ic4YFx87U-moxnT0xKw7m-rbNqU-EZd5ixk$
> > > PLEASE do read the posting guide https://urldefense.com/v3/__http://www.R-project.org/posting-guide.html__;!!KwNVnqRv!Qe700OYnXI-Qe4XYE55MkVqMX3g4Ic4YFx87U-moxnT0xKw7m-rbNqU-fWVQ0xI$
> > > and provide commented, minimal, self-contained, reproducible code.
> >
> > ______________________________________________
> > [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> > https://urldefense.com/v3/__https://stat.ethz.ch/mailman/listinfo/r-help__;!!KwNVnqRv!Qe700OYnXI-Qe4XYE55MkVqMX3g4Ic4YFx87U-moxnT0xKw7m-rbNqU-EZd5ixk$
> > PLEASE do read the posting guide https://urldefense.com/v3/__http://www.R-project.org/posting-guide.html__;!!KwNVnqRv!Qe700OYnXI-Qe4XYE55MkVqMX3g4Ic4YFx87U-moxnT0xKw7m-rbNqU-fWVQ0xI$
> > and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Beginner problem - using mod function to print odd numbers

Rolf Turner
In reply to this post by R help mailing list-2
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: Beginner problem - using mod function to print odd numbers

Greg Minshall
In reply to this post by Bert Gunter-2
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: Beginner problem - using mod function to print odd numbers

Martin Maechler
In reply to this post by David Carlson
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: Beginner problem - using mod function to print odd numbers

Eric Berger
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: Beginner problem - using mod function to print odd numbers

Bill Dunlap-2
In reply to this post by Martin Maechler
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: [EXT] Re: Beginner problem - using mod function to print odd numbers

dkStevens
This thread, in addition to being a nice Thursday morning chuckle,
should be de rigueur for 'R 101' users. It illustrates much of the
thinking behind R in a very simple way, without taking itself too
seriously. Well done all!

David Stevens
**/**/

On 6/9/2021 8:35 AM, Bill Dunlap wrote:

> Martin wrote
>    Use
>        num[num %% 2 == 1]
>    instead of much slower and ...@#^$
>        num[ifelse(num %% 2 == 1, TRUE, FALSE)]
>
> Read the '[' as 'such that' when the subscript is logical
> (=="Boolean"==TRUE/FALSE-values).
>
> [The original post had a typo/thinko, num<-num+i instead of num<-num+1,
> which was simply an error, not a matter of style.  R's vectorization makes
> it easy to avoid such errors.]
>
> -Bill
>
> On Wed, Jun 9, 2021 at 2:56 AM Martin Maechler <[hidden email]>
> wrote:
>
>>>>>>> David Carlson    on Sun, 6 Jun 2021 15:21:34 -0400 writes:
>>> There is really no need for a loop:
>>> num <- 1:100
>>> num[ifelse(num %% 2 == 1, TRUE, FALSE)]
>>> [1]  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
>>> [26] 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
>>
>> Well, and the above "works" but is really another proof of my
>> year-long claim that people use  ifelse(.)  *MUCH MUCH* too often,
>> and should really learn to use alternatives, in this case,
>> "R 101" (*long* before fooverse):
>>
>> Use
>>
>>      num[num %% 2 == 1]
>>
>> instead of much slower and ...@#^$
>>
>>      num[ifelse(num %% 2 == 1, TRUE, FALSE)]
>>
>> Martin Maechler
>> ETH Zurich
>>
>>> On Sat, Jun 5, 2021 at 2:05 PM William Michels via R-help
>>> <[hidden email]> wrote:
>>      >>
>>      >> > i <- 1L; span <- 1:100; result <- NA;
>>      >> > for (i in span){
>>      >> + ifelse(i %% 2 != 0, result[i] <- TRUE, result[i] <- FALSE)
>>      >> + }
>>      >> > span[result]
>>      >> [1]  1  3  5  7  9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41
>> 43
>>
>>   [............]
>>
>> ______________________________________________
>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>          [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> CAUTION: This email originated from outside of USU. If this appears to be a USU employee, beware of impersonators. Do not click links, reply, download images, or open attachments unless you verify the sender’s identity and know the content is safe.
>
--
David K Stevens, PhD,PE
Professor
Civil and Environmental Engineering
Utah State University
Logan, UT 84322-8200
[hidden email]
014357973229


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.