Dear All! I'm looking for equivalent of Matematica function "Which" which works as follows: z = Which[x<10,0.3, 10<=x<20,0.5, 20<=x<100,1] where x is a vector I can replace it with custom function with set of ifelse but I'm looking for simpler and faster (much faster) solution best wishes Jarek
Henrique Dallazuanna
2009-Sep-29 18:16 UTC
[R] Equivalent for Matematica function Which...
Try this; cut(x, breaks = c(0, 10, 20, 100), labels = c(0.3, .5, 1)) On Tue, Sep 29, 2009 at 3:11 PM, Jarek Jasiewicz <jarekj at amu.edu.pl> wrote:> Dear All! > > I'm looking for equivalent of Matematica function "Which" which works as > follows: > > z = Which[x<10,0.3, 10<=x<20,0.5, 20<=x<100,1] > > where x is a vector > > I can replace it with custom function with set of ifelse but I'm looking for > simpler and faster (much faster) solution > > best wishes > Jarek > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
Hi Jarek, Take a look at ?which, ?ifelse and ?recode (car package). HTH, Jorge On Tue, Sep 29, 2009 at 2:11 PM, Jarek Jasiewicz <> wrote:> Dear All! > > I'm looking for equivalent of Matematica function "Which" which works as > follows: > > z = Which[x<10,0.3, 10<=x<20,0.5, 20<=x<100,1] > > where x is a vector > > I can replace it with custom function with set of ifelse but I'm looking > for simpler and faster (much faster) solution > > best wishes > Jarek > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
Hello,> I'm looking for equivalent of Matematica function "Which" which works as > follows: > > z = Which[x<10,0.3, 10<=x<20,0.5, 20<=x<100,1] > > where x is a vectorUnless someone happens to be a Mathematica user (very possible), I don't know how we would answer the question. You give an example of the function call, but not what the function arguments or return value are. There is an R function called "which", but what the Mathematica "Which" is doing is entirely mysterious from your example. What is in "z" for instance??