nelpar
2021-Jun-02 18:17 UTC
[R] Beginner problem - using mod function to print odd numbers
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
Hasan Diwan
2021-Jun-05 06:19 UTC
[R] Beginner problem - using mod function to print odd numbers
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 <leavesofnyc at gmail.com> 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 at 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
<http://bit.ly/hd1AppointmentRequest>*.
Si vous voudrais faire connnaisance, allez a *bit.ly/hd1AppointmentRequest
<http://bit.ly/hd1AppointmentRequest>*.
<https://sks-keyservers.net/pks/lookup?op=get&search=0xFEBAD7FFD041BBA1>Sent
from my mobile device
Envoye de mon portable
[[alternative HTML version deleted]]
Jeff Newmiller
2021-Jun-05 07:44 UTC
[R] Beginner problem - using mod function to print odd numbers
What if you used num <- num + 1 ? On June 2, 2021 11:17:50 AM PDT, nelpar <leavesofnyc at gmail.com> 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 at 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.
Stefan Evert
2021-Jun-05 07:51 UTC
[R] Beginner problem - using mod function to print odd numbers
> > 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?
Rui Barradas
2021-Jun-05 12:54 UTC
[R] Beginner problem - using mod function to print odd numbers
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 at 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.
>