最後の要素が返されるように、関数の最後にvect
を追加します。
wins <- function(vect, prob = c(0.01, 0.99)){
#vect is a vector with values to be winsorized
#prob contains top and bottom percentiles at which to winsorize data in vect
low_quantile <- quantile(vect, probs = prob[1], na.rm = TRUE)
high_quantile <- quantile(vect, probs = prob[2], na.rm = TRUE)
vect[vect < low_quantile] <- low_quantile
vect[vect > high_quantile] <- high_quantile
vect
}
wins(1:100)
[1] 1.99 2.00 3.00 4.00 5.00 6.00 7.00 8.00 9.00 10.00 11.00 12.00 13.00 14.00 15.00 16.00 17.00 18.00
[19] 19.00 20.00 21.00 22.00 23.00 24.00 25.00 26.00 27.00 28.00 29.00 30.00 31.00 32.00 33.00 34.00 35.00 36.00
[37] 37.00 38.00 39.00 40.00 41.00 42.00 43.00 44.00 45.00 46.00 47.00 48.00 49.00 50.00 51.00 52.00 53.00 54.00
[55] 55.00 56.00 57.00 58.00 59.00 60.00 61.00 62.00 63.00 64.00 65.00 66.00 67.00 68.00 69.00 70.00 71.00 72.00
[73] 73.00 74.00 75.00 76.00 77.00 78.00 79.00 80.00 81.00 82.00 83.00 84.00 85.00 86.00 87.00 88.00 89.00 90.00
[91] 91.00 92.00 93.00 94.00 95.00 96.00 97.00 98.00 99.00 99.01
EDIT data.frameにこれを適用する方法についてのフォローアップの質問:あなたはあなたのフォローアップに入れてそれはまた、単一の列で動作します
df1 <- data.frame(matrix(1:200,ncol=2))
apply(df1,2,wins) # apply by column
> apply(df1,2,wins)
X1 X2
[1,] 1.99 101.99
[2,] 2.00 102.00
[3,] 3.00 103.00
[4,] 4.00 104.00
[5,] 5.00 105.00
...
:
wins(df1$X1)
[1] 1.99 2.00 3.00 4.00 5.00 6.00 7.00 8.00 9.00 10.00 11.00 12.00 13.00 14.00 15.00 16.00 17.00 18.00
[19] 19.00 20.00 21.00 22.00 23.00 24.00 25.00 26.00 27.00 28.00 29.00 30.00 31.00 32.00 33.00 34.00 35.00 36.00
[37] 37.00 38.00 39.00 40.00 41.00 42.00 43.00 44.00 45.00 46.00 47.00 48.00 49.00 50.00 51.00 52.00 53.00 54.00
[55] 55.00 56.00 57.00 58.00 59.00 60.00 61.00 62.00 63.00 64.00 65.00 66.00 67.00 68.00 69.00 70.00 71.00 72.00
[73] 73.00 74.00 75.00 76.00 77.00 78.00 79.00 80.00 81.00 82.00 83.00 84.00 85.00 86.00 87.00 88.00 89.00 90.00
[91] 91.00 92.00 93.00 94.00 95.00 96.00 97.00 98.00 99.00 99.01
おそらく、関数内で起こっていることが、関数外のオブジェクトに魔法を働かせていると思うかもしれません。彼らはしない。明示的にvectを返し、関数の結果を新しいオブジェクトまたは既存のオブジェクトに割り当てる必要があります。 – joran