Re: [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 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. > -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O __ 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.
Re: [R] Equivalent for Matematica function Which...
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]] __ 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.
Re: [R] Equivalent for Matematica function Which...
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 vector Unless 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?? __ 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.
Re: [R] Equivalent for Matematica function Which...
William Dunlap pisze: -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Jarek Jasiewicz Sent: Tuesday, September 29, 2009 11:36 AM To: Erik Iverson Cc: R-help@r-project.org Subject: Re: [R] Equivalent for Matematica function Which... well function arguments are in square brackets. z is result (new vector). I show Matematica syntax, but cannot explain what I expect. Sorry The example is wrong because it can be replaced by R cut function. The arguments are: condition,action and can be replaced by ste of ifelse formulas: if (x<10) x<-0.7 else if (x<30 && x=>10) x<-x^2/(x-1) etc... but that solution is slow for vectors with millions of numbers ifelse is different than if-then-else. Your if-then-else needs to be in a loop but ifelse is vectorized. Try something like z <- ifelse(x<10, 0.7,# result for x's less than 10 ifelse(x<30 & x>=10, # x>=10 is redundant in this branch x^2/(x-1), # result for x's >=10 and x<30 1)) # result for x's>=30 That evaluates all the arguments for each value in x, even those for which the answer will not be used. That wastes some time and sometimes causes warnings or even errors. In that case you can use more flexible but less convenient syntax like: z <- NA * x # initialize z to be like x but filled with NA's cond <- x<10 z[cond] <- 0.7 cond <- x>=10 & x<30 z[cond] <- x[cond]^2/(x[cond]-1) # or (function(y)y^2/(y-1))(x[cond]) cond <- x>=30 z[cond] <- 1.0 Thanks! it seems very good approach 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.
Re: [R] Equivalent for Matematica function Which...
On Sep 29, 2009, at 2:36 PM, Jarek Jasiewicz wrote: well function arguments are in square brackets. z is result (new vector). I show Matematica syntax, but cannot explain what I expect. Sorry The example is wrong because it can be replaced by R cut function. The arguments are: condition,action and can be replaced by ste of ifelse formulas: if (x<10) x<-0.7 else if (x<30 && x=>10) x<-x^2/(x-1) etc... but that solution is slow for vectors with millions of numbers 1)Slow? It shouldn't even work! The if { } else { } construct is for program control, not for iterative testing and assignment. You should be using ifelse(, , ) which is designed for that purpose. 2) you should not be using "&&" unless you are working with a scalar. Use "&". Perhaps (minimally tested): x <- 1:100 ifelse( x < 10, 0.7, ifelse( x<30 & x>=10, x^2/(x-1), NA)) (And the "x >= 10" , (I doubted that " =>" would be correct and testing shows it does throw a syntax error), is not needed since you would never get to that evaluation if x were < 10. Note: the ifelse's can be only nested up to 7 levels if memory serves. -- David thanks Jarek Erik Iverson pisze: 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 vector Unless 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?? __ 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. David Winsemius, MD Heritage Laboratories West Hartford, CT __ 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.
Re: [R] Equivalent for Matematica function Which...
> -Original Message- > From: r-help-boun...@r-project.org > [mailto:r-help-boun...@r-project.org] On Behalf Of Jarek Jasiewicz > Sent: Tuesday, September 29, 2009 11:36 AM > To: Erik Iverson > Cc: R-help@r-project.org > Subject: Re: [R] Equivalent for Matematica function Which... > > well function arguments are in square brackets. z is result (new > vector). I show Matematica syntax, but cannot explain what I > expect. Sorry > The example is wrong because it can be replaced by R cut > function. The > arguments are: condition,action and can be replaced by > ste of ifelse > formulas: > > if (x<10) x<-0.7 > else if (x<30 && x=>10) x<-x^2/(x-1) > etc... > but that solution is slow for vectors with millions of numbers ifelse is different than if-then-else. Your if-then-else needs to be in a loop but ifelse is vectorized. Try something like z <- ifelse(x<10, 0.7,# result for x's less than 10 ifelse(x<30 & x>=10, # x>=10 is redundant in this branch x^2/(x-1), # result for x's >=10 and x<30 1)) # result for x's>=30 That evaluates all the arguments for each value in x, even those for which the answer will not be used. That wastes some time and sometimes causes warnings or even errors. In that case you can use more flexible but less convenient syntax like: z <- NA * x # initialize z to be like x but filled with NA's cond <- x<10 z[cond] <- 0.7 cond <- x>=10 & x<30 z[cond] <- x[cond]^2/(x[cond]-1) # or (function(y)y^2/(y-1))(x[cond]) cond <- x>=30 z[cond] <- 1.0 > > thanks > > Jarek > > Erik Iverson pisze: > > 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 vector > >> > > > > Unless 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?? > > > > __ > 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. > __ 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.
Re: [R] Equivalent for Matematica function Which...
well function arguments are in square brackets. z is result (new vector). I show Matematica syntax, but cannot explain what I expect. Sorry The example is wrong because it can be replaced by R cut function. The arguments are: condition,action and can be replaced by ste of ifelse formulas: if (x<10) x<-0.7 else if (x<30 && x=>10) x<-x^2/(x-1) etc... but that solution is slow for vectors with millions of numbers thanks Jarek Erik Iverson pisze: 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 vector Unless 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?? __ 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.
Re: [R] Equivalent for Matematica function Which...
well thanks, when I post the mail I thought I got too simple example which may be really replaced by "cut", but I thought about little more: let say: z = Which[x<10,x/3, 10<=x<20,0.5, 20<=x<100,x^2/(x-1)] where there are both values and formula sorry for mismatch and thanks for quick answer Jarek Henrique Dallazuanna pisze: 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 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. __ 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.
[R] Equivalent for Matematica function Which...
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.