Identifying several variable outliers with rstatix

409 Views Asked by At

Here is the dput for the data I have. I have only included the head of the data because this is a pretty massive dataset, but I think it should suffice given my question:

structure(list(Prioritising.workload = c(2L, 2L, 2L, 4L, 1L, 
2L), Writing.notes = c(5L, 4L, 5L, 4L, 2L, 3L), Workaholism = c(4L, 
5L, 3L, 5L, 3L, 3L), Reliability = c(4L, 4L, 4L, 3L, 5L, 3L), 
    Self.criticism = c(1L, 4L, 4L, 5L, 5L, 4L), Loneliness = c(3L, 
    2L, 5L, 5L, 3L, 2L), Changing.the.past = c(1L, 4L, 5L, 5L, 
    4L, 3L), Number.of.friends = c(3L, 3L, 3L, 1L, 3L, 3L), Mood.swings = c(3L, 
    4L, 4L, 5L, 2L, 3L), Socializing = c(3L, 4L, 5L, 1L, 3L, 
    4L), Energy.levels = c(5L, 3L, 4L, 2L, 5L, 4L), Interests.or.hobbies = c(3L, 
    3L, 5L, NA, 3L, 5L)), row.names = c(NA, 6L), class = "data.frame")

I am trying to find outliers for all of these variables. If I do this individually, I will get the following code that is as long as the Nile River:

#### EFA Personality Data Check ####
ef.personality %>% 
  identify_outliers(Prioritising.workload) %>% 
  select(is.extreme)
ef.personality %>% 
  identify_outliers(Writing.notes) %>% 
  select(is.extreme)
ef.personality %>% 
  identify_outliers(Workaholism) %>% 
  select(is.extreme)
ef.personality %>% 
  identify_outliers(Reliability) %>% 
  select(is.extreme)
ef.personality %>% 
  identify_outliers(Self.criticism) %>% 
  select(is.extreme)
ef.personality %>% 
  identify_outliers(Loneliness) %>% 
  select(is.extreme)
ef.personality %>% 
  identify_outliers(Changing.the.past) %>% 
  select(is.extreme)
ef.personality %>% 
  identify_outliers(Number.of.friends) %>% 
  select(is.extreme)
ef.personality %>% 
  identify_outliers(Mood.swings) %>% 
  select(is.extreme)
ef.personality %>% 
  identify_outliers(Socializing) %>% 
  select(is.extreme)
ef.personality %>% 
  identify_outliers(Energy.levels) %>% 
  select(is.extreme)
ef.personality %>% 
  identify_outliers(Interests.or.hobbies) %>% 
  select(is.extreme)

Is there some command I can use to make this a lot simpler? I was thinking of some kind of loop that can check each variable and return outliers for each, but I'm not sure how to achieve that. I am also open to solutions that dont rely on rstatix.

1

There are 1 best solutions below

3
On BEST ANSWER

The beauty of rstatix is that it is pipe friendly. So, you can use it with tidyverse framework. tidyverse requires the data in long-form. You can use the following code

library(tidyverse)
library(rstatix)

ef.personality %>% 
  mutate(id = seq(1, nrow(ef.personality),1)) %>% #To create a unique column required to make that data in long form 
  pivot_longer(-id) %>% #To make the data in long form required for `tidyverse`
  group_by(name) %>% #Based on which column you want aggregate 
  identify_outliers(value) %>% 
  select(name, is.extreme)