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
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 > >
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. 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 >
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 factor data structure is more useful directly. (Factors are sort of an acquired taste.) Note that in the call to cut, I am passing many arguments into the function by their position in the call. You will need to look at help("cut") to figure out which argument is which. Note also that by monkeying with the two logical arguments, ("include.lowest" and "right"), I didn't need to fudge any of the cutpoints. tmp <- range(x) tmp <- c(tmp[1], 250, 500, 750, tmp[2]) fac <- cut(x, tmp, c("red","blue","green","black"), TRUE, FALSE) col <- as.character(fac) HTH - tom blackwell - u michigan medical school - ann arbor - On Mon, 1 Dec 2003, 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 >
Just some small refinements/corrections: colours <- c("red", "blue", "green","back") colours[cut(x, c(-Inf,250,500,700,Inf),right=F,lab=F)] --- Date: Tue, 02 Dec 2003 14:38:55 +1300 From: Hadley Wickham <h.wickham at auckland.ac.nz> To: Arend P. van der Veen <apv at capital.net> Cc: R HELP <r-help at stat.math.ethz.ch> 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[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. 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 >
And one other thing. Are you sure you want character variables as the result of all this? A column whose entries are each one of four colors seems like 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 <ggrothendieck at myway.com> To: <h.wickham at auckland.ac.nz>, <apv at capital.net> Cc: <r-help at stat.math.ethz.ch> Subject: Re: [R] Vector Assignments Just some small refinements/corrections: colours <- c("red", "blue", "green","back") colours[cut(x, c(-Inf,250,500,700,Inf),right=F,lab=F)] --- Date: Tue, 02 Dec 2003 14:38:55 +1300 From: Hadley Wickham <h.wickham at auckland.ac.nz> To: Arend P. van der Veen <apv at capital.net> Cc: R HELP <r-help at stat.math.ethz.ch> 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[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. 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 >
Your recommendations have worked great. I have found both cut and ifelse to be useful. I have one more question. When should I use factors over a character vector. I know that they have different uses. However, I am still trying to figure out how I can best take advantage of factors. The following is what I am really trying to do: colors <- c("red","blue","green","black") y.col <- colors[cut(y,c(-Inf,250,500,700,Inf),right=F,lab=F)] plot(x,y,col=y.col) Would using factors make this any cleaner? I think a character vector is all I need but I thought I would ask. Thanks for your help, Arend van der Veen On Tue, 2003-12-02 at 00:32, Gabor Grothendieck wrote:> And one other thing. Are you sure you want character variables > as the result of all this? A column whose entries are each one > of four colors seems like 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 <ggrothendieck at myway.com> > To: <h.wickham at auckland.ac.nz>, <apv at capital.net> > Cc: <r-help at stat.math.ethz.ch> > Subject: Re: [R] Vector Assignments > > > > > > Just some small refinements/corrections: > > colours <- c("red", "blue", "green","back") > colours[cut(x, c(-Inf,250,500,700,Inf),right=F,lab=F)] > > --- > Date: Tue, 02 Dec 2003 14:38:55 +1300 > From: Hadley Wickham <h.wickham at auckland.ac.nz> > To: Arend P. van der Veen <apv at capital.net> > Cc: R HELP <r-help at stat.math.ethz.ch> > 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[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. 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 > > > > > _______________________________________________ > No banners. No pop-ups. No kidding. > Introducing My Way - http://www.myway.com >
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 <apv at capital.net> To: R HELP <r-help at stat.math.ethz.ch> Subject: Re: [R] Vector Assignments Your recommendations have worked great. I have found both cut and ifelse to be useful. I have one more question. When should I use factors over a character vector. I know that they have different uses. However, I am still trying to figure out how I can best take advantage of factors. The following is what I am really trying to do: colors <- c("red","blue","green","black") y.col <- colors[cut(y,c(-Inf,250,500,700,Inf),right=F,lab=F)] plot(x,y,col=y.col) Would using factors make this any cleaner? I think a character vector is all I need but I thought I would ask. Thanks for your help, Arend van der Veen On Tue, 2003-12-02 at 00:32, Gabor Grothendieck wrote:> And one other thing. Are you sure you want character variables > as the result of all this? A column whose entries are each one > of four colors seems like 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 <ggrothendieck at myway.com> > To: <h.wickham at auckland.ac.nz>, <apv at capital.net> > Cc: <r-help at stat.math.ethz.ch> > Subject: Re: [R] Vector Assignments > > > > > > Just some small refinements/corrections: > > colours <- c("red", "blue", "green","back") > colours[cut(x, c(-Inf,250,500,700,Inf),right=F,lab=F)] > > --- > Date: Tue, 02 Dec 2003 14:38:55 +1300 > From: Hadley Wickham <h.wickham at auckland.ac.nz> > To: Arend P. van der Veen <apv at capital.net> > Cc: R HELP <r-help at stat.math.ethz.ch> > 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[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. 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 > > > > > _______________________________________________ > No banners. No pop-ups. No kidding. > Introducing My Way - http://www.myway.com >
Thomas W Blackwell
2003-Dec-03 13:56 UTC
[R] reason for Factors -- was -- Vector Assignments
On Wed, 3 Dec 2003, Arend P. van der Veen wrote:> Your recommendations have worked great. I have found both cut and > ifelse to be useful. > > I have one more question. When should I use factors over a character > vector. I know that they have different uses. However, I am still > trying to figure out how I can best take advantage of factors. > > The following is what I am really trying to do: > > colors <- c("red","blue","green","black") > y.col <- colors[cut(y,c(-Inf,250,500,700,Inf),right=F,lab=F)] > plot(x,y,col=y.col) > > Would using factors make this any cleaner? I think a character vector > is all I need but I thought I would ask. > > Thanks for your help, > Arend van der VeenArend - When setting the colors of plotted points, you definitely want a vector of character strings as the color names. "Factor" was invented so that regression and analysis of variance functions would properly recognize a grouping variable and not fit simply a linear coefficient to the integer codes. In the context of a linear (or similar) model, each factor or interaction has to be expanded from a single column of integer codes into a matrix of [0,1] indicator variables, with a separate column for each possible level of the factor. (I oversimplify a bit here: some columns are omitted, to keep the design matrix from being over-specified.) - tom blackwell - u michigan medical school - ann arbor -