claushojmark
claushojmark

Reputation: 27

Function to subset and adjust vector

The function is supposed to take a vector and winsorize values at the 1st and 99th percentile (replacing values larger 99th percentile with the 99th percentile, vice versa for values lower than the 1st percentile). I can run the function without any errors, but it does not change the vector that is given as argument. When I run the same code outside the function, it works fine, but I have to do it for many columns in a data.frame, so I would like to be able to pass the function through an apply function.

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
}

Any suggestions?

Upvotes: 0

Views: 108

Answers (1)

Pierre Lapointe
Pierre Lapointe

Reputation: 16277

Add vect at the end of your function so that the last element is returned.

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 Follow-up question on how to apply this to a 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
...

It also works with a single column as you put in your follow-up:

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

Upvotes: 1

Related Questions