r - How to remove outliers from a list of vectors? -
i have list of vectors :
tdatm.sp=structure(list(x3co = c(24.88993835, 25.02366257, 24.90308762 ), x3cs = c(25.70629883, 25.26747704, 25.1953907), x3cd = c(26.95723343, 26.84725571, 26.2314415), x3csd = c(36.95250702, 36.040905, 36.90475845 ), x5co = c(25.44123077, 24.97585869, 24.86075592), x5cs = c(25.71570396, 26.10244179, 25.39032555), x5cd = c(27.67508507, 27.18985558, 26.93682098), x5csd = c(36.26528549, 34.88553238, 33.97910309 ), x7co = c(24.7142601, 24.08443642, 23.97057915), x7cs = c(24.55734444, 24.56562042, 24.7589817), x7cd = c(27.14260101, 26.65704346, 26.49533081), x7csd = c(33.89881897, 32.91091919, 32.79199219 ), x9co = c(26.86141014, 26.42648888, 25.8350563), x9cs = c(28.17367744, 27.27400589, 26.58813667), x9cd = c(28.88915062, 28.32597542, 28.2713623), x9csd = c(34.61352158, 35.84189987, 35.80329132)), .names = c("x3co", "x3cs", "x3cd", "x3csd", "x5co", "x5cs", "x5cd", "x5csd", "x7co", "x7cs", "x7cd", "x7csd", "x9co", "x9cs", "x9cd", "x9csd")) > head(tdatm.sp) $x3co [1] 24.88994 25.02366 24.90309 $x3cs [1] 25.70630 25.26748 25.19539 $x3cd [1] 26.95723 26.84726 26.23144 $x3csd [1] 36.95251 36.04091 36.90476 $x5co [1] 25.44123 24.97586 24.86076 $x5cs [1] 25.71570 26.10244 25.39033
i remove outliers each individual vector using hampel method.
one way found :
repoutliers=function(x){ med=median(x); mad=mad(x); x[x>med+3*mad | x<med-3*mad]=na; return(x)} lapply(tdatm.sp, repoutliers)
but wondering if possible without declaring new function, directly within lapply. lapply sends each individual vector function repoutliers, know how operate on individual vectors directly within lapply ? let's swap repoutliers function "replace", same word calling individual vectors in arguments of replace (lapply(x,fun,...); ... = replace arguments).
in brief : how manipulate individual vectors lapply sends function winthin lapply ?
it's more or less tomato tomahtoe thing. doing in lapply doesn't far.
lapply( tdatm.sp, function(x){ med=median(x) mad=mad(x) x[x>med+3*mad | x<med-3*mad]=na return(x)} )
now lapply
sending anonymous function. if didn't want function hanging around afterwards handy syntax.
Comments
Post a Comment