[R] A question about function behavior
McGehee, Robert
Robert.McGehee at geodecapital.com
Wed Apr 20 20:33:22 CEST 2005
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
More information about the R-help
mailing list