简体   繁体   中英

I want a function in R to check whether values of one column are greater than the 75th quantile then writes yes or no in the next column

I have tried the following formula but it gives all nos even when I change the quantile value. NOTE: I have 3 independent datasets that I want to apply the function.

outlier<-function(x1,x2){
  q1<-quantile(x1 , .75, na.rm = TRUE)
    if(x1>q1){x2<-"Yes"
    }else{
      x2<-"No"
    
  }

}

I have tried x2<-ifelse(x1>q1,"Yes","No") inside the function but it still doesn't work.

You can use an ifelse statement and create a new column using mutate .

library(dplyr)
set.seed(1)

df <- tibble(x1 = sample(c(1:10), size = 10, replace = T))

df %>% 
  mutate(x2 = ifelse(quantile(x1, 0.75, na.rm = T) < x1, "Yes", "No"))

If you want a function

library(dplyr)
set.seed(1)

df <- tibble(x1 = sample(c(1:10), size = 10, replace = T), 
             x2 = sample(c(1:10), size = 10, replace = T),
             x3 = sample(c(1:10), size = 10, replace = T),
             x4 = sample(c(1:10), size = 10, replace = T))




outlier<-function(dataframe, quant = 0.75, col = c("x1", "x2")){
  
  dataframe %>% 
    mutate(across(all_of(col), ~ifelse(.x>quantile(.x,0.75), 'Yes', 'No'),
                  .names = '{col}_yes'))

  }

outlier(dataframe = df,quant =  0.25)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM