Sunday, 15 May 2011

r - Clipping elements in a long vector to +/-threshold -



r - Clipping elements in a long vector to +/-threshold -

i writing programme in r. stuck here.

i have vector

x=c(84.05, 108.04, 13.95, -194.05, 64.03, 208.05, 84.13, 57.04)

i want vector after replacing elements of vector >180 180 , elements less <-180 -180.

like want get,

x=c(84.05, 108.04, 13.95,-180, 64.03, 180, 84.13, 57.04)

how this??

the vector working large.

try using pmin

> pmin(abs(x), 180)*sign(x) [1] 84.05 108.04 13.95 -180.00 64.03 180.00 84.13 57.04

benchmark

> jilber <- function() pmin(abs(x), 180)*sign(x) > mrflick <- function() pmin(pmax(x, -180), 180) > user1317221_g <- function() ifelse(x < -180,-180, ifelse(x > 180, 180, x)) > benchmark(replications=50000, + jilber(), + mrflick(), + user1317221_g(), + columns=c('test', 'elapsed', 'relative')) test elapsed relative 1 jilber() 0.835 1.000 2 mrflick() 1.297 1.553 3 user1317221_g() 1.709 2.047

r algorithm clipping

No comments:

Post a Comment