我有这个向量列表:
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
我想使用 Hampel 方法从每个单独的向量中删除异常值。
我发现这样做的一种方法是:
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)
但我想知道是否可以直接在 lapply 中声明一个新函数而不必这样做。lapply 将每个单独的向量发送到函数repoutliers,您知道如何在 lapply 中直接对这些单独的向量进行操作吗?假设我用函数“replace”交换repoutliers,我可以通过调用replace参数中的各个向量来做同样的事情(lapply(X,FUN,...); ... =替换参数)。
简而言之:如何操作单个向量 lapply 发送到函数 winthin lapply ?