Add ifelse() created variable to list of data frames

Viewed 68

I want to know how to create a variable using the ifelse() command to a list of data frames.

I created 2 datasets, using the 300 top and bottom values from the ggplot2::diamonds dataset, called diamonds_top300 and diamonds_bottom300:

# Loads packages
# ---- NOTE: making plots and diamonds dataset
if(!require(ggplot2)){install.packages("ggplot2")}
# ---- NOTE: for data wrangling
if(!require(dplyr)){install.packages("dplyr")}

# dataset creation
# ---- NOTE: selects only the top 300 rows of the dataset
diamonds_top300 <- data.frame(dplyr::top_n(diamonds, 300, table))
# ---- NOTE: selects only the bottom 300 rows of the dataset
diamonds_bottom300 <- data.frame(dplyr::top_n(diamonds, -300, table))

I then used lapply and functions to create a list with 2 models, only differing on dataset used:

# Loads packages
# ---- NOTE: run mixed effects models
if(!require(lme4)){install.packages("lme4")}

## lists datasets to use
DATASET_list <- c("diamonds_top300", "diamonds_bottom300")

## creates model
# ---- NOTE: creates list object
freq_mlm_poisson_model <- 
  lapply(DATASET_list,
         function(data_list) wrapr::let(
           c(data_list_model = data_list), 
           (lme4::glmer(
             price ~ cut + color + carat + (1 | clarity) + (1 | depth),
             data = data_list_model,
             family = poisson()
           )
           )
         )
  )
# ---- NOTE: changes list object name
freq_mlm_poisson_model <- 
  setNames(freq_mlm_poisson_model, paste("freq_mlm_poisson_model", 
                                         DATASET_list,
                                              sep = "__")
  )

I then used lapply and functions to create the summary of these models in list form:

### creates summary model for list object freq_mlm_poisson_model
# ---- NOTE: creates list object
freq_mlm_poisson_summary <- 
  lapply(
    freq_mlm_poisson_model, 
    function(model_list) {
      summary(model_list)
    }
  )
# ---- NOTE: changes list object name
freq_mlm_poisson_summary <- 
  setNames(freq_mlm_poisson_summary, paste("freq_mlm_poisson_summary", 
                                           DATASET_list,
                                         sep = "__")
  )

I then turned the summary into a data frame with just the fixed effects information:

### turns summary list fixed effects into list of data frames
# ---- NOTE: creates object with summary of fixed effects
freq_mlm_poisson_summary_fixedeffects <- 
  lapply(freq_mlm_poisson_summary, `[[`, 10)
# ---- NOTE: creates list object
freq_mlm_poisson_summary_fixedeffects_df <- 
  lapply(
    freq_mlm_poisson_summary_fixedeffects, 
    function(model_list) {
      data.frame(model_list)
    }
  )
# ---- NOTE: changes list object name
freq_mlm_poisson_summary_fixedeffects_df <- 
  setNames(freq_mlm_poisson_summary_fixedeffects_df, paste("freq_mlm_poisson_summary_fixedeffects_df", 
                                           DATASET_list,
                                           sep = "__")
  )

Is there a way to use the ifelse() command or some other command to create variable called p_value_sign in each of the lists, which tell yes/no if the corresponding p value in the variable Pr(>|z|) is less that 0.05 (i.e., p < 0.05)?

1 Answers

As it is a list of data.frame, we can directly create the column with transform after looping over the list with lapply. It may be better to keep it as logical vector because it helps in doing subsets easier

freq_mlm_poisson_summary_fixedeffects_df2 <- lapply(
       freq_mlm_poisson_summary_fixedeffects_df,
       transform, p_value_sign = `Pr...z..` < 0.05)

If we need multiple comparisons, use case_when

library(dplyr)
library(purrr)
library(stringr)
freq_mlm_poisson_summary_fixedeffects_df2 <- map(
   freq_mlm_poisson_summary_fixedeffects_df, ~ .x %>%
        mutate(p_value_sign = case_when(`Pr...z..` < 0.05 ~ 
          "significant at p < 0.05",
           ( `Pr...z..` < 0.10) & (`Pr...z..` >= 0.05) ~ 
           "marginally significant at p < 0.10", TRUE ~ "Not significant")))
Related