Skip to content

How to define desired numbers to a vector based on the present numbers

6 messages · Rui Barradas, suparna mitra, Clint Bowman +1 more

#
Hello,

I'm not sure I understand. You want to attribute a color number 1:7 to 
each element of your vector? Maybe the following will do.


x <- scan(text = "
43 43 43 43  0 39 13 39 50 39 39 23 23 32  0 13 32 23 32 23  0 13 13  0
")

cols <- rep(1:7, rle(x[order(x)])$lengths)[order(order(x))]
plot(x, col = cols)


Hope this helps,

Rui Barradas

Em 21-06-2013 16:13, Suparna Mitra escreveu:
#
I suspect the OP may want

rep(1:length(unique(x)), rle(x[order(x)])$lengths)[order(order(x))]

to allow for variable numbers of unique values.

Clint

Clint Bowman			INTERNET:	clint at ecy.wa.gov
Air Quality Modeler		INTERNET:	clint at math.utah.edu
Department of Ecology		VOICE:		(360) 407-6815
PO Box 47600			FAX:		(360) 407-7534
Olympia, WA 98504-7600

         USPS:           PO Box 47600, Olympia, WA 98504-7600
         Parcels:        300 Desmond Drive, Lacey, WA 98503-1274
On Fri, 21 Jun 2013, Rui Barradas wrote:

            
#
HI,
May be this also works:
plot(x,col=as.numeric(factor(x)))
A.K.




----- Original Message -----
From: Rui Barradas <ruipbarradas at sapo.pt>
To: Suparna Mitra <suparna.mitra.sm at gmail.com>
Cc: r-help at r-project.org
Sent: Friday, June 21, 2013 11:32 AM
Subject: Re: [R] How to define desired numbers to a vector based on the
 present numbers

Hello,

I'm not sure I understand. You want to attribute a color number 1:7 to 
each element of your vector? Maybe the following will do.


x <- scan(text = "
43 43 43 43? 0 39 13 39 50 39 39 23 23 32? 0 13 32 23 32 23? 0 13 13? 0
")

cols <- rep(1:7, rle(x[order(x)])$lengths)[order(order(x))]
plot(x, col = cols)


Hope this helps,

Rui Barradas

Em 21-06-2013 16:13, Suparna Mitra escreveu:
______________________________________________
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.