Pergunta

I have this 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 would like to remove outliers from each individual vector using the Hampel method.

One way I found to do it is :

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 I was wondering if it was possible to do it without declaring a new function, directly within lapply. lapply sends each individual vector to the function repoutliers, do you know how to operate on this individual vectors directly within lapply ? Let's say I swap repoutliers with the function "replace", I could do the same word by calling the individual vectors in the arguments of replace (lapply(X,FUN,...); ... = replace arguments).

In brief : how to manipulate individual vectors lapply sends to the function winthin lapply ?

Foi útil?

Solução

It's really more or less a tomato tomahtoe thing. Doing it all in lapply doesn't get you very 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 is just sending everything to an anonymous function. But if you didn't want the function hanging around afterwards this is handy syntax.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top