For , Replace Values

hello friends ,

df <- data.frame(diamonds)

I managed to do the calculation manually for 1 column :
pct <- quantile(df$table, c(0.01, 0.99), type = 1) # define minimum & maximum based on1% and 99%
df$table[df$table>=pct] <- max(pct) # replace any value greater than pct with max(pct)
df$table[df$table<=pct] <- min(pct) # replace any value less than pct with min(pct)

i want to run the same code ,,but for the entire table from column 4 to 10

I tried using for function , but i didn't manage to do it .

any suggestion

Thank you

I think this gets you what you want.

library(tidyverse)
DF <- data.frame(Nm = "A", 
                 V1 = rnorm(1000),
                 V2 = rnorm(1000),
                 V3 = rnorm(1000),
                 Nm2 = "B")
summary(DF)
#>       Nm                  V1                  V2                  V3           
#>  Length:1000        Min.   :-3.445319   Min.   :-2.901110   Min.   :-3.451585  
#>  Class :character   1st Qu.:-0.649255   1st Qu.:-0.713068   1st Qu.:-0.650663  
#>  Mode  :character   Median : 0.033550   Median : 0.009566   Median : 0.001472  
#>                     Mean   :-0.009692   Mean   :-0.026401   Mean   : 0.016894  
#>                     3rd Qu.: 0.659536   3rd Qu.: 0.684788   3rd Qu.: 0.680270  
#>                     Max.   : 2.618920   Max.   : 3.206873   Max.   : 3.963105  
#>      Nm2           
#>  Length:1000       
#>  Class :character  
#>  Mode  :character  
#>                    
#>                    
#> 

#For comparison to the final result
quantile(DF$V1, c(0.01, 0.99), type = 1)
#>       1%      99% 
#> -2.51800  2.11393
quantile(DF$V2, c(0.01, 0.99), type = 1)
#>        1%       99% 
#> -2.621235  2.302048
quantile(DF$V3, c(0.01, 0.99), type = 1)
#>        1%       99% 
#> -2.444660  2.340334


RplFunc <- function(Vec) {
  pct <- quantile(Vec, c(0.01, 0.99), type = 1)
  Vec <- ifelse(Vec > pct[2], pct[2], Vec)
  Vec <- ifelse(Vec < pct[1], pct[1], Vec)
  return(Vec)
}

DF <- DF |> mutate(across(.cols = 2:4, .fns = RplFunc))
summary(DF)
#>       Nm                  V1                  V2                  V3           
#>  Length:1000        Min.   :-2.518000   Min.   :-2.621235   Min.   :-2.444660  
#>  Class :character   1st Qu.:-0.649255   1st Qu.:-0.713067   1st Qu.:-0.650663  
#>  Mode  :character   Median : 0.033550   Median : 0.009566   Median : 0.001472  
#>                     Mean   :-0.009373   Mean   :-0.029653   Mean   : 0.016492  
#>                     3rd Qu.: 0.659536   3rd Qu.: 0.684788   3rd Qu.: 0.680270  
#>                     Max.   : 2.113930   Max.   : 2.302048   Max.   : 2.340334  
#>      Nm2           
#>  Length:1000       
#>  Class :character  
#>  Mode  :character  
#>                    
#>                    
#> 

Created on 2023-02-24 with reprex v2.0.2

1 Like

thank you so much for your continuous contribution and support .

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.