[R] Is there a better way ?

Joshua Wiley jwiley.psych at gmail.com
Mon Jul 11 08:19:37 CEST 2011


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:
> 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.
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.
>



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
https://joshuawiley.com/



More information about the R-help mailing list