[R] which.pmin?

Huntsinger, Reid reid_huntsinger at merck.com
Mon Jan 24 19:44:02 CET 2005


That's what "ifelse" is for:

ifelse(fpr(b, k.floor) < fpr(b, k.ceiling), k.floor, k.ceiling)

Reid Huntsinger



-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Seung Jun
Sent: Friday, January 21, 2005 5:27 PM
To: R-help at stat.math.ethz.ch
Subject: [R] which.pmin?


I have two vectors (k.floor and k.ceiling) of integers of the same
length, and a function (fpr).

    b <- 10:40
    k.floor <- floor(log(2) * b)
    k.ceiling <- ceiling(log(2) * b)
    fpr.floor <- fpr(b, k.floor)
    fpr.ceiling <- fpr(b, k.ceiling)

If R had a element-wise ternary function, I'd like to do something like 
this:

    (fpr.floor < fpr.ceiling) ? k.floor : k.ceiling

That is, I'd like to go through the two vectors in parallel, picking
the one that returns the lower value of fpr. Failing to find such a
function, I wrote the following two lines:

    ind <- sapply(data.frame(rbind(fpr.floor,fpr.ceiling)), which.min)
    opt.k <- cbind(k.floor,k.ceiling)[1:length(ind)+length(ind)*(ind-1)]

opt.k is the vector I want, but I guess I abuse some functions here.
I'd like to ask the experts, What is the proper R-way to do this?

The API should be like "which.pmin(FUN, X, Y, ...)" that returns a
vector of the same length as X (and Y), provided that X, Y, ... have
the same length. Please fill the function body.

Seung

______________________________________________
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