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
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:1
> 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 8
> 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 modalitie
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]
> [
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
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 P
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 s
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 "vectori
> 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
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
>
> 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 o
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]
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 7
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 close
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
[
16 matches
Mail list logo