[R] RGB function

Duncan Murdoch murdoch at stats.uwo.ca
Mon Jul 31 06:57:31 CEST 2006

On 7/31/2006 12:36 AM, Kartik Pappu wrote:
> Hi all,
> I created three separate square matrices (lets say R, G, and B).  each
> one contains a series of values between 0 and 1.  I want to be able to
> take for example R[1], G[1], B[1] and create a rgb color value into a
> fourth matrix.  I tried using the rgb function but I must be doing
> something wrong because it fails with the following error message.
> (list) object cannot be coerced to 'double'.
> Any ideas on how I could do this.

I just tried what you said, and it worked:

> r <- matrix(runif(100), 10,10)
> g <- matrix(runif(100), 10,10)
> b <- matrix(runif(100), 10,10)
> matrix(rgb(r,g,b),10,10)

The final call to matrix() is needed because rgb() loses the dimension 
attribute from its result.

Duncan Murdoch

More information about the R-help mailing list