Skip to content

Instantly share code, notes, and snippets.

@guidocor
Created September 28, 2016 12:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guidocor/726291e786edce11d2cffb60184f3bfd to your computer and use it in GitHub Desktop.
Save guidocor/726291e786edce11d2cffb60184f3bfd to your computer and use it in GitHub Desktop.
Gets the IQR and 1.5 range of the distribution of each member or of class
iqr_1.5 <- function(df, rt, conds, FUN = function(x) {
c(iqr = IQR(x),
lower = quantile(x, c(0.25))-1.5*(quantile(x, c(0.75))-quantile(x, c(0.25))),
upper = quantile(x, c(0.75))+1.5*(quantile(x, c(0.75))-quantile(x, c(0.25))))
}){
# This function calculates the 1.5 * IQR of each participant and condition
# returns a dataframe with the columns formatted.
# Is used to detect outliers in reaction times at participant per condition level.
# Also, yo can use it with custom functions
require(doBy)
if(!require("doBy")){stop("The doBy package is needed")}
by_conds <- paste(conds, collapse = " + ")
formula_specificated <-as.formula(paste0( rt, "~", by_conds ) )
new_df <- summaryBy(
formula = formula_specificated,
data = df,
FUN = FUN)
new_df<-merge(df, new_df, conds)
return(new_df)
}
convert_factor <- function(df, columns){
df[,columns] <- lapply(df[,columns], as.factor)
return(df)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment