If you were using the colours in a model matrix
using a factor would be best but since your case
is plotting using characters is best.
Date: 03 Dec 2003 08:26:05 -0500
From: Arend P. van der Veen <[EMAIL PROTECTED]>
To: R HELP <[EMAIL PROTECTED]>
Subject: Re: [R] Vector
a good job for a factor:
>
> colours <- c("red", "blue", "green","black")
> cut(x, c(-Inf,250,500,700,Inf),right=F,lab=colours)
>
>
>
> ---
> Date: Mon, 1 Dec 2003 23:47:39 -0500 (EST)
> From: Gabor Grothendieck <[EM
f),right=F,lab=colours)
---
Date: Mon, 1 Dec 2003 23:47:39 -0500 (EST)
From: Gabor Grothendieck <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>, <[EMAIL PROTECTED]>
Cc: <[EMAIL PROTECTED]>
Subject: Re: [R] Vector Assignments
Just some small refinements/corrections:
d P. van der Veen <[EMAIL PROTECTED]>
Cc: R HELP <[EMAIL PROTECTED]>
Subject: Re: [R] Vector Assignments
One way would be to create a vector of colours and then cut() to index
the vector:
colours <- c("red", "blue", "green","back")
colours[c
Arend -
Here is a sequence of commands which will do it.
These first build a vector of (4+1) cutpoints,
then cut() returns a factor whose labels are
the colors and codes are determined by x. Last,
as.character() turns the factor into the character
vector which you ask for. Or, perhaps the fa
One way would be to create a vector of colours and then cut() to index
the vector:
colours <- c("red", "blue", "green","back")
colours[cut(x, c(min(x),250,500,700,max(x)),lab=F)]
Hadley
Arend P. van der Veen wrote:
Hi,
I have simple R question.
I have a vector x that contains real numbers.
One way is to use "ifelse":
x <- seq(100, 1000, by=100)
x.col <- ifelse(x < 250, "red",
ifelse(x<500, "blue", ifelse(x<750, "green", "black")))
data.frame(x, x.col)
x x.col
1 100 red
2 200 red
3 300 blue
4 400 blue
5 500 green
6 600 green
7 700 green
8 800 black
Hi,
I have simple R question.
I have a vector x that contains real numbers. I would like to create
another vector col that is the same length of x such that:
if x[i] < 250 then col[i] = "red"
else if x[i] < 500 then col[i] = "blue"
else if x[i] < 750 then col[i] = "green"
else col[i] = "black