Re: [R] y needing more than 2 functions
On Mon, Mar 26, 2012 at 07:48:07PM -0400, Aimee Jones wrote: > Dear all, > > I'm aware if y has two separate functions (depending on the conditions > of x) you can use the ifelse function to separate y into two separate > functions depending on input. How do you do this if there a multiple > different conditions for x? > > for example, > > y fits the following between t>0 & t<15->function(t) t^2, y fits > the following between t>15 & t<30-> function(t) t^3, y fits the > following between t>30 &t<45--->function(t) t^4 etc Hi. Try the following. bounds <- c(0, 15, 30, 45) x <- seq(4, 44, length=51) valfunc <- cbind(x^2, x^3, x^4) indfunc <- findInterval(x, bounds) y <- valfunc[cbind(1:nrow(valfunc), indfunc)] # verify range(x[y == x^2]) # [1] 4.0 14.4 range(x[y == x^3]) # [1] 15.2 29.6 range(x[y == x^4]) # [1] 30.4 44.0 Hope this helps. Petr Savicky. __ [email protected] 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] y needing more than 2 functions
Another approach:
foo <- function(t){
bm <- ceiling(t/15)
s <- cut(t,breaks=15*(0:bm),labels=1:bm)
s <- as.numeric(levels(s)[s])
t^(s+1)
}
This idea can be generalised .
cheers,
Rolf Turner
On 27/03/12 15:31, R. Michael Weylandt wrote:
One way is to simply nest your ifelse()s:
y<- ifelse(t< 15, t^2, ifelse(t< 30, t^3, t^4))
Michael
On Mon, Mar 26, 2012 at 7:48 PM, Aimee Jones
wrote:
Dear all,
I'm aware if y has two separate functions (depending on the conditions
of x) you can use the ifelse function to separate y into two separate
functions depending on input. How do you do this if there a multiple
different conditions for x?
for example,
y fits the following between t>0& t<15->function(t) t^2, y fits
the following between t>15& t<30-> function(t) t^3, y fits the
following between t>30&t<45--->function(t) t^4 etc
Thanks for any help you are able to give,
yours sincerely,
Aimee
__
[email protected] 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.
__
[email protected] 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.
__
[email protected] 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] y needing more than 2 functions
One way is to simply nest your ifelse()s: y <- ifelse(t < 15, t^2, ifelse(t < 30, t^3, t^4)) Michael On Mon, Mar 26, 2012 at 7:48 PM, Aimee Jones wrote: > Dear all, > > I'm aware if y has two separate functions (depending on the conditions > of x) you can use the ifelse function to separate y into two separate > functions depending on input. How do you do this if there a multiple > different conditions for x? > > for example, > > y fits the following between t>0 & t<15->function(t) t^2, y fits > the following between t>15 & t<30-> function(t) t^3, y fits the > following between t>30 &t<45--->function(t) t^4 etc > > > Thanks for any help you are able to give, > yours sincerely, > Aimee > > __ > [email protected] 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. __ [email protected] 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] y needing more than 2 functions
Dear all, I'm aware if y has two separate functions (depending on the conditions of x) you can use the ifelse function to separate y into two separate functions depending on input. How do you do this if there a multiple different conditions for x? for example, y fits the following between t>0 & t<15->function(t) t^2, y fits the following between t>15 & t<30-> function(t) t^3, y fits the following between t>30 &t<45--->function(t) t^4 etc Thanks for any help you are able to give, yours sincerely, Aimee __ [email protected] 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.

