mardi 30 mars 2021

Change column values when specific condition is satisfied

I have the problem following on which I'm thinking for a while and cannot figure out the solution.

Let's consider some artificial data frame and it's 0.9 and 0.1 quantiles:

set.seed(42)
x = data.frame("Norm" = rnorm(100),
               "Unif" = runif(100),
               "Exp" = rexp(100))

quants_b <- apply(x, 2, quantile, 0.90)
quants_s <- apply(x, 2, quantile, 0.10)

What I want to do in this data frame is to check which values are bigger than its corresponding quantile 0.9 and less than corresponding quantile 0.1 and changed all those values to the limits.

In simpler words:

I want to check which values exceed 0.9 quantile and all those values transform to 0.9 quantile

and I want to do the same with 0.1 quantile.

Troublesomeness of the problem

This problem in my opinion looks very easy at the first sight, however it has one trap - we have to do the transformation in the same time, because if we firstly change the upper bound and then lower, between the transformation quantiles can change.

(Please notice that we want to replace first variable with element of quants_s and quants_b, second with second and so on).

My ideas

My first idea was to use dplyr package and a function mutate_all within it.

x %>% dplyr::mutate_all(
  function(x) {
    ifelse(sweep(x, 2,STATS=quants_s, `<`), quants_s,
           ifelse(sweep(x, 2,STATS=quants_b, `>`), 
                 quants_b, x)
    )
  }
)

This code intuitively is very simple - we just change all values which are lower than quants_s with quants_s, and those which are bigger than quants_b with quants_b. Remaining data stays the same. However I got error following and I'm not sure how to omit it:

Error: Problem with `mutate()` input `Norm`.
x 'dims' cannot be of length 0
i Input `Norm` is `(function (x) ...`.
Run `rlang::last_error()` to see where the error occurred.

Could you please give me a hand solving the problem/pointing another solution ?

Aucun commentaire:

Enregistrer un commentaire