[R] Vector Assignments
Spencer Graves
spencer.graves at pdf.com
Tue Dec 2 02:32:47 CET 2003
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
9 900 black
10 1000 black
Does this seem reasonable?
spencer graves
Arend P. van der Veen wrote:
>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" for all i
>
>I am convinced that there is probably a very efficient way to do this in
>R but I am not able to figure it out. Any help would be greatly
>appreciated.
>
>Thanks in advance,
>Arend van der Veen
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>
>
More information about the R-help
mailing list