How to replace outliers with the 5th and 95th percentile values in R

前端 未结 4 1110
野性不改
野性不改 2020-12-30 11:26

I\'d like to replace all values in my relatively large R dataset which take values above the 95th and below the 5th percentile, with those percentile values

4条回答
  •  我在风中等你
    2020-12-30 12:16

    I used this code to get what you need:

    qn = quantile(df$value, c(0.05, 0.95), na.rm = TRUE)
    df = within(df, { value = ifelse(value < qn[1], qn[1], value)
                      value = ifelse(value > qn[2], qn[2], value)})
    

    where df is your data.frame, and value the column that contains your data.

提交回复
热议问题