Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vectorizing "if" in R

Tags:

indexing

r

I have an array of probability over a classification

post <- c(0.73,0.69,0.44,0.55,0.67,0.47,0.08,0.15,0.45,0.35)

and I want to retrieve the predicted class. right now I use

predicted <- function(post) { 
               function(threshold) {plyr::aaply(post, 1, 
                   function(x) {if(x >= threshold) '+' else '-'})}}

but that seems like something R would have a syntax for.

Is there some indexing expression that would be more direct ?

like image 908
nicolas Avatar asked Sep 22 '26 11:09

nicolas


1 Answers

pred <- c("-", "+")[1+(post > 0.5)]
like image 147
IRTFM Avatar answered Sep 24 '26 02:09

IRTFM