Hello, I have been trying to figure this one out, but don't seem to go anywhere. I have a function like this: a = function(t) { max(0,t+1) } very simple, but if I pass a vector of n values to this function I expect n evaluations of max and instead I get only one value (the largest value of them all..). Is there anyway to do this without invoking a for loop? thanks, Jorge
Perhaps by using apply? What do you think that max(0,c(1,2,3,4)+1) should return? On 4/20/05, Jorge Ahumada <jahumada at usgs.gov> wrote:> Hello, > > I have been trying to figure this one out, but don't seem to go > anywhere. I have a function like this: > > a = function(t) { > > max(0,t+1) > > } > > very simple, but if I pass a vector of n values to this function I > expect n evaluations of max and instead I get only one value (the > largest value of them all..). Is there anyway to do this without > invoking a for loop? > > thanks, > > Jorge > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >-- best, -tony "Commit early,commit often, and commit in a repository from which we can easily roll-back your mistakes" (AJR, 4Jan05). A.J. Rossini blindglobe at gmail.com
> From: Jorge Ahumada > > Hello, > > I have been trying to figure this one out, but don't seem to go > anywhere. I have a function like this: > > a = function(t) { > > max(0,t+1) > > } > > very simple, but if I pass a vector of n values to this function I > expect n evaluations of max and instead I get only one value (the > largest value of them all..). Is there anyway to do this without > invoking a for loop?It works as documented. ?max says: Value max and min return the maximum or minimum of all the values present in their arguments, as integer if all are integer, or as double otherwise. What you want is pmax(). [This should perhaps be included in the "See Also" section of ?max.] Andy> thanks, > > Jorge > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > > >
Sure. Here are three ways. Using pmax() is probably the most elegant. a <- function(t) { pmax(t + 1, 0) } OR a <- function(t) { sapply(t + 1, max, 0) } OR a <- function(t) { t <- t + 1 t[which(t < 0)] <- 0 t } -----Original Message----- From: Jorge Ahumada [mailto:jahumada at usgs.gov] Sent: Wednesday, April 20, 2005 1:03 PM To: r-help at stat.math.ethz.ch Subject: [R] A question about function behavior Hello, I have been trying to figure this one out, but don't seem to go anywhere. I have a function like this: a = function(t) { max(0,t+1) } very simple, but if I pass a vector of n values to this function I expect n evaluations of max and instead I get only one value (the largest value of them all..). Is there anyway to do this without invoking a for loop? thanks, Jorge ______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
?pmax David L. Reiner -----Original Message----- From: Jorge Ahumada [mailto:jahumada at usgs.gov] Sent: Wednesday, April 20, 2005 12:03 PM To: r-help at stat.math.ethz.ch Subject: [R] A question about function behavior Hello, I have been trying to figure this one out, but don't seem to go anywhere. I have a function like this: a = function(t) { max(0,t+1) } very simple, but if I pass a vector of n values to this function I expect n evaluations of max and instead I get only one value (the largest value of them all..). Is there anyway to do this without invoking a for loop? thanks, Jorge ______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
I think you want this: b<- function(t) {pmax(0,t+1)} alex ----- Original Message ----- From: "Jorge Ahumada" <jahumada at usgs.gov> To: <r-help at stat.math.ethz.ch> Sent: Wednesday, April 20, 2005 6:02 PM Subject: [R] A question about function behavior> Hello, > > I have been trying to figure this one out, but don't seem to go > anywhere. I have a function like this: > > a = function(t) { > > max(0,t+1) > > } > > very simple, but if I pass a vector of n values to this function I > expect n evaluations of max and instead I get only one value (the > largest value of them all..). Is there anyway to do this without > invoking a for loop? > > thanks, > > Jorge > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide!http://www.R-project.org/posting-guide.html>
Jorge Ahumada wrote on 4/20/2005 12:02 PM:> Hello, > > I have been trying to figure this one out, but don't seem to go > anywhere. I have a function like this: > > a = function(t) { > > max(0,t+1) > > } > > very simple, but if I pass a vector of n values to this function I > expect n evaluations of max and instead I get only one value (the > largest value of them all..). Is there anyway to do this without > invoking a for loop? > > thanks, > > Jorge >I think you want ?pmax instead of ?max. --sundar
Hello Consider ?pmax instead of max a <- function(t){ pmax(0,t+1) } a( c(1,2,-4,2)) Romain Le 20.04.2005 19:02, Jorge Ahumada a ??crit :> Hello, > > I have been trying to figure this one out, but don't seem to go > anywhere. I have a function like this: > > a = function(t) { > > max(0,t+1) > > } > > very simple, but if I pass a vector of n values to this function I > expect n evaluations of max and instead I get only one value (the > largest value of them all..). Is there anyway to do this without > invoking a for loop? > > thanks, > > Jorge-- ~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~ ~~~~~~ Romain FRANCOIS - http://addictedtor.free.fr ~~~~~~ ~~~~ Etudiant ISUP - CS3 - Industrie et Services ~~~~ ~~ http://www.isup.cicrp.jussieu.fr/ ~~ ~~~~ Stagiaire INRIA Futurs - Equipe SELECT ~~~~ ~~~~~~ http://www.inria.fr/recherche/equipes/select.fr.html ~~~~~~ ~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~