user297850
user297850

Reputation: 8015

R command with sequential usages of <-

I once read the following R command

 lambda1.post[s]<-lambda1<-rgamma(1,a0+sum(Y[1:k]),b0+k)

In the above command, what does the term in the middle, i.e., lambda1 used for? What is its difference with the following one

lambda1.post[s]<-rgamma(1,a0+sum(Y[1:k]),b0+k)

Upvotes: 1

Views: 69

Answers (1)

Greg Snow
Greg Snow

Reputation: 49640

The assignment operator (<-) is right associative and returns (invisibly) the value that it assigns. Therefore:

a <- b <- c <- d

is the same as:

a <- ( b <- ( c <- d ) )

and is equivalent to:

c <- d
b <- c # or b <- d
a <- b # or a <- c or a <- d

This is just a shorthand for assigning the same value to multiple variables in one step.

Upvotes: 2

Related Questions