Skip to content

Is there a better way ?

2 messages · eric, Joshua Wiley

#
Is there a more compact way to say this ?

r <-numeric(length(p)) ; s <-numeric(length(p)); t <- numeric(length(p)); u
<- numeric(length(p)); v <- numeric(length(p)) ; x <-numeric(length(p))

all these variables will be used in a loop 

for (i in 1 : length(p)) {
r[i] <-
s[i] <-
t[i] <-
etc
}

--
View this message in context: http://r.789695.n4.nabble.com/Is-there-a-better-way-tp3658588p3658588.html
Sent from the R help mailing list archive at Nabble.com.
#
Hi Eric,

You can use chain assignment, for example:

r <- s <- t <- u <- v <- x <- numeric(length(p))

Also, just an FYI, you can do something similar using vector(), for example:

vector(mode = "numeric", length = length(p))

of course you would not need to explicitly name the arguments.  There
is no particular benefit in this case, but vector() can create
different classes of vectors, which is convenient in many cases.

HTH,

Josh

P.S. for loops are sometimes not the most efficient way of
accomplishing a given task, though it depends on many factors.  If you
think there might be a way to vectorize the loop, you could always ask
for any ideas.
On Sun, Jul 10, 2011 at 7:29 PM, eric <ericstrom at aol.com> wrote: