-1

In the following example, how can i.want.them.all get all the 10 numbers from 5 to 14?

control.me <- 0 # or 1
a.lot.of.elements <- c(5:14)
i.want.them.all <- ifelse(control.me == 1, a.lot.of.elements, a.lot.of.elements - 10)
print(i.want.them.all)
[1] 5

Is there a construct that works like this? :

a.lot.of.elements <- c(5:14)
i.want.them.now <- magic.construct(control.me == 1, a.lot.of.elements, a.lot.of.elements - 10)
print(i.want.them.now)
[1]  5  6  7  8  9 10 11 12 13 14

If not, how could I accomplish what I want to do?

Konstantinos
  • 4,096
  • 3
  • 19
  • 28
  • 1
    If your condition results in a `length == 1` "logical", you could use a simple `if(test) yes else no`. `?ifelse` states that "test" determines most attributes (including `length`) of the output – alexis_laz Apr 10 '16 at 21:44
  • You should probably read `help(ifelse)` – Rich Scriven Apr 10 '16 at 21:57

1 Answers1

0

Perhaps just testing seq_along since any positive integer will always evaluate to TRUE:

> i.want.them.now <- ifelse(seq_along(a.lot.of.elements), a.lot.of.elements, character())
> i.want.them.now 
 [1]  5  6  7  8  9 10 11 12 13 14
IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • sorry, i updated my question. there should be a control structure as the first argument of ifelse() – Konstantinos Apr 10 '16 at 21:53
  • `ifelse` is a function returning values of the same length as its first argument ... while `if` is a control construct (although also a function). There was a comment (since deleted) suggesting you could use `if(control.me==1){a.lot.of.elements}` . (You will not get very far suggesting modifications to `ifelse`.) – IRTFM Apr 10 '16 at 22:30