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

2021-06-09 Thread Bill Dunlap
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 
wrote:

> > David Carlsonon 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
> >  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
>
>  []
>
> __
> R-help@r-project.org 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]]

__
R-help@r-project.org 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.


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

2021-06-09 Thread Eric Berger
It's also possible to save a character and gain the added advantage of
being less understandable :-)

num[!!num%%2]



On Wed, Jun 9, 2021 at 12:56 PM Martin Maechler 
wrote:

> > David Carlsonon 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
> >  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
>
>  []
>
> __
> R-help@r-project.org 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]]

__
R-help@r-project.org 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.


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

2021-06-09 Thread Martin Maechler
> David Carlsonon 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
>  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

 []

__
R-help@r-project.org 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.


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

2021-06-08 Thread Greg Minshall
> Two of R's central features as a "data science" language are that many of
> its core capabilities are "vectorized" -- can calculate on whole objects
> (at the user-visible interpreter level) rather than requiring explicit
> loops; and that it can use object indexing in several different modalities,
> here logical indexing, for extraction and replacement in whole objects such
> as vectors and matrices. Not only does this typically yield simpler, more
> readable code (admittedly, a subjective judgment), but it is also typically
> much faster, though I grant you that this can often be overrated.

i sort of wanted to circle back to this.

i learned FORTRAN IV in 1968 (and the last FORTRAN program i wrote was
at least 50 years ago), and i *still* write FORTRAN code, even when
writing lisp, or R, or awk scripts.  it's *very* hard for me to adapt to
other styles.

so, for that reason, i think it is very useful to help people understand
and adjust to R's vector operations.

maybe a way to say it might be "here is how you will get better
performance, using R's vector operations..." and, then, "however, if you
find it simpler, and the performance acceptable, you could do a 'for'
loop like this...".  (though, as this is all volunteer effort, one
obviously takes what one gets.)  but, i do think that helping newer
users understand how to write efficient code is important.

anyway, just my two cents.

cheers, Greg

__
R-help@r-project.org 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.


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

2021-06-06 Thread Rolf Turner
On Sun, 6 Jun 2021 04:34:02 -0700
William Michels via R-help  wrote:




> ... But of course, a more simple approach than I previously
> posted would be below (although less idiomatic than your answer):
> 
> > object <- 1:100
> > index <- ifelse(object %% 2 == 1, TRUE, FALSE)
> > object[index]
>  [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

Why faff around with ifelse()?

object[object %% 2 == 1]

works just fine.



cheers,

Rolf Turner

-- 
Honorary Research Fellow
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276

__
R-help@r-project.org 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.


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

2021-06-06 Thread 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  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
>  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  
> > 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?
> > >
> > > __
> > > R-help@r-project.org 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.
> >
> > __
> > R-help@r-project.org 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.

__
R-help@r-project.org 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.


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

2021-06-06 Thread David Carlson
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
 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  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?
> >
> > __
> > R-help@r-project.org 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.
>
> __
> R-help@r-project.org 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.

__
R-help@r-project.org 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.


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

2021-06-06 Thread William Michels via R-help
Dear Bert,

First off, I want to thank you for the many hundreds (if not
thousands) of excellent posts I've read from you on this mailing list
over the years. And you are absolutely correct that when using the
`%%` modulo operator, your code is the most compact and the most
idiomatic.

That being said, if someone coming from another programming language
is posting code on this mailing list using a `for()` loop,  they may
be most comfortable getting working code back from this mailing-list
that still uses a `for()` loop in R. Furthermore, people often start
by *filtering* their data, when a better approach might be to first
*recode* it, for which the `ifelse()` function provides a nice
solution. But of course, a more simple approach than I previously
posted would be below (although less idiomatic than your answer):

> object <- 1:100
> index <- ifelse(object %% 2 == 1, TRUE, FALSE)
> object[index]
 [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
>

This brings us to the question of programming 'philosophy':  certain
languages advise "TMTOWTDI", while another advises "There should be
one--and preferably only one--obvious way to do it." Where does R fit
on this spectrum? I believe one of R's underappreciated strengths is
being more in-line with the "TMTOWTDI" principle. So people coming
from other languages can get good results right off the bat using
vectorized indexing/filtering, or for() loops, or the apply() family
of functions, or writing a custom function (all answers given in this
thread).

Finally, if a nascent R programmer ever ventures into filtering their
data using objects and indexes of different lengths, they should have
a grasp of the code examples below (recycling rule):

long_vec <- 1:16
print(long_vec)
short_vec <- rep(4,8)
print(short_vec)
print(long_vec[long_vec > short_vec])

span <- 1:length(short_vec)
print(long_vec[span][long_vec[span] > short_vec])
print(long_vec[span][long_vec > short_vec])

Best Regards, Bill.

W. Michels, Ph.D.



On Sat, Jun 5, 2021 at 12:26 PM Bert Gunter  wrote:
>
> I'm sorry, but  this is a good example of how one should *not* do this in R. 
> I also should apologize for any pedantry that follows, but I believe this 
> serves as a nice example of the ideas.
>
> Two of R's central features as a "data science" language are that many of its 
> core capabilities are "vectorized" -- can calculate on whole objects (at the 
> user-visible interpreter level) rather than requiring explicit loops; and 
> that it can use object indexing in several different modalities, here logical 
> indexing, for extraction and replacement in whole objects such as vectors and 
> matrices. Not only does this typically yield simpler, more readable code 
> (admittedly, a subjective judgment), but it is also typically much faster, 
> though I grant you that this can often be overrated.
>
> In this instance, the several lines of looping code you presented can be 
> condensed into a single line:
>
> > span <- 1:20
> > span[span %% 2 == 1]
>  [1]  1  3  5  7  9 11 13 15 17 19
>
> ### Trickier, but perhaps instructive, is: ###
> > span[TRUE & span %% 2]
>  [1]  1  3  5  7  9 11 13 15 17 19
>
> All languages trade off various strengths and weaknesses, but I think it's 
> fair to say that one should try to work within the paradigms that are the 
> language's strengths when possible, R's vectorization and indexing in this 
> example.
>
> Cheers,
> Bert Gunter
>
> "The trouble with having an open mind is that people keep coming along and 
> sticking things into it."
> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>
>
> On Sat, Jun 5, 2021 at 11:05 AM William Michels via R-help 
>  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  
>> 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?
>> >
>> > __
>> > R-help@r-project.org 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 

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

2021-06-05 Thread Bert Gunter
I'm sorry, but  this is a good example of how one should *not* do this in
R. I also should apologize for any pedantry that follows, but I believe
this serves as a nice example of the ideas.

Two of R's central features as a "data science" language are that many of
its core capabilities are "vectorized" -- can calculate on whole objects
(at the user-visible interpreter level) rather than requiring explicit
loops; and that it can use object indexing in several different modalities,
here logical indexing, for extraction and replacement in whole objects such
as vectors and matrices. Not only does this typically yield simpler, more
readable code (admittedly, a subjective judgment), but it is also typically
much faster, though I grant you that this can often be overrated.

In this instance, the several lines of looping code you presented can be
condensed into a single line:

> span <- 1:20
> span[span %% 2 == 1]
 [1]  1  3  5  7  9 11 13 15 17 19

### Trickier, but perhaps instructive, is: ###
> span[TRUE & span %% 2]
 [1]  1  3  5  7  9 11 13 15 17 19

All languages trade off various strengths and weaknesses, but I think it's
fair to say that one should try to work within the paradigms that are the
language's strengths when possible, R's vectorization and indexing in this
example.

Cheers,
Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Sat, Jun 5, 2021 at 11:05 AM William Michels via R-help <
r-help@r-project.org> 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 
> 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?
> >
> > __
> > R-help@r-project.org 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.
>
> __
> R-help@r-project.org 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]]

__
R-help@r-project.org 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.


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

2021-06-05 Thread William Michels via R-help
> 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  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?
>
> __
> R-help@r-project.org 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.

__
R-help@r-project.org 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.


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

2021-06-05 Thread Rui Barradas

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

__
R-help@r-project.org 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.



__
R-help@r-project.org 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.


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

2021-06-05 Thread Stefan Evert
> 
> 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?

__
R-help@r-project.org 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.


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

2021-06-05 Thread Jeff Newmiller
What if you used

num <- num + 1

?

On June 2, 2021 11:17:50 AM PDT, nelpar  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)
>}
>
>
>[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
>
>__
>R-help@r-project.org 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.

-- 
Sent from my phone. Please excuse my brevity.

__
R-help@r-project.org 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.


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

2021-06-05 Thread Eric Berger
Typo
num <- num + i
should be
num <- num + 1


On Sat, Jun 5, 2021 at 9:38 AM Hasan Diwan  wrote:

> unlist(sapply(seq(1,100), function(n) { if(n %% 2) n })) yields:
>
>  [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
>
> As for why your solution isn't working, if you'd like me to take a closer
> look and figure out exactly what you're doing wrong, let me know? -- H
>
> On Fri, 4 Jun 2021 at 23:09, nelpar  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)
> > }
> >
> >
> > [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
> >
> > __
> > R-help@r-project.org 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.
> >
>
>
> --
> OpenPGP: https://hasan.d8u.us/openpgp.asc
> If you wish to request my time, please do so using
> *bit.ly/hd1AppointmentRequest
> *.
> Si vous voudrais faire connnaisance, allez a *bit.ly/hd1AppointmentRequest
> *.
>
>  >Sent
> from my mobile device
> Envoye de mon portable
>
> [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org 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]]

__
R-help@r-project.org 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.


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

2021-06-05 Thread Hasan Diwan
unlist(sapply(seq(1,100), function(n) { if(n %% 2) n })) yields:

 [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

As for why your solution isn't working, if you'd like me to take a closer
look and figure out exactly what you're doing wrong, let me know? -- H

On Fri, 4 Jun 2021 at 23:09, nelpar  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)
> }
>
>
> [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
>
> __
> R-help@r-project.org 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.
>


-- 
OpenPGP: https://hasan.d8u.us/openpgp.asc
If you wish to request my time, please do so using
*bit.ly/hd1AppointmentRequest
*.
Si vous voudrais faire connnaisance, allez a *bit.ly/hd1AppointmentRequest
*.

Sent
from my mobile device
Envoye de mon portable

[[alternative HTML version deleted]]

__
R-help@r-project.org 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.


[R] Beginner problem - using mod function to print odd numbers

2021-06-05 Thread 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

__
R-help@r-project.org 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.