Skip to content
Prev 281028 / 398513 Next

Loops and vector operations

Look at ?ifelse. You'll need to nest them however. If you are feeling a little more ambitious this can be done with ?switch, but for clarity, I'd rather see pre-allocation followed by assignments  Something along this pattern:

out <- numeric(50)
x <- sample(3, 50, TRUE)
out[x==3] <- "A"
out[x==2] <- "B"
out[x==1] <- "C"

Not the fastest, but quite clear

HTH,

Michael
On Dec 28, 2011, at 10:55 AM, Mago84 <davidquiasmo at gmail.com> wrote: