Search code examples
rdatatabletitlesjplot

How to assign title to series of tables


I'm going to create a series of table

library(sjPlot)    
tables_models <- lapply(models_list_2, tab_model, show.ci = FALSE, 
           show.se = TRUE, 
           show.p = TRUE, 
           show.stat = TRUE, 
           show.df = TRUE, 
           show.ngroups = TRUE,
           digits = 4,
           digits.p = 3, 
           digits.re = 3,
           **title = title**,
           string.pred = "Predictors",
           string.est = "Estimate",
           string.se = "std. Error",
           string.p = "Pr(>|t|)",
           string.df = "df",
           string.stat = "t-value",
           string.intercept = "(Intercept)", 
           df.method = 'kr', 
           p.val = 'kr', 
           p.style = 'numeric_stars') %>% 
      setNames(sort(unique(out_long$signals)))

Where the title input should assign a title like 'Description of....' and then proper related signals' names which it represents:

['P3(400-450).FCz', 'P3(400-450).Cz', 'P3(400-450).Pz', 'LPPearly(500-700).FCz', 'LPPearly(500-700).Cz', 'LPPearly(500-700).Pz', 'LPP1(500-1000).FCz', 'LPP1(500-1000).Cz', 'LPP1(500-1000).Pz', 'LPP2(1000-1500).FCz', 'LPP2(1000-1500).Cz', 'LPP2(1000-1500).Pz', 'LPP2(1000-1500).POz']

For title I just had a try creating the following object,

    title <- c('P3(400-450).FCz', 'P3(400-450).Cz', 'P3(400-450).Pz',
      'LPPearly(500-700).FCz', 'LPPearly(500-700).Cz',
      'LPPearly(500-700).Pz', 'LPP1(500-1000).FCz', 
      'LPP1(500-1000).Cz', 'LPP1(500-1000).Pz', 
      'LPP2(1000-1500).FCz', 'LPP2(1000-1500).Cz',
     'LPP2(1000-1500).Pz', 'LPP2(1000-1500).POz')

That just run as it is, the first code names each table with the first names indicated into the brackets, or 'P3(400-450).FCz'

How should I set out the title input for assign each name reported into the title object for a different table?

Thanks for paying attention

Here the dataset I'm working

dput(head(out_long, 50))
structure(list(ID = c("01", "01", "01", "04", "04", "04", "06", 
"06", "06", "07", "07", "07", "08", "08", "08", "09", "09", "09", 
"10", "10", "10", "11", "11", "11", "12", "12", "12", "13", "13", 
"13", "15", "15", "15", "16", "16", "16", "17", "17", "17", "18", 
"18", "18", "19", "19", "19", "21", "21", "21", "22", "22"), 
    GR = c("RP", "RP", "RP", "RP", "RP", "RP", "RP", "RP", "RP", 
    "RP", "RP", "RP", "RP", "RP", "RP", "RP", "RP", "RP", "RP", 
    "RP", "RP", "RP", "RP", "RP", "RP", "RP", "RP", "RP", "RP", 
    "RP", "RP", "RP", "RP", "RP", "RP", "RP", "RP", "RP", "RP", 
    "RP", "RP", "RP", "RP", "RP", "RP", "RP", "RP", "RP", "RP", 
    "RP"), SES = c("V", "V", "V", "V", "V", "V", "V", "V", "V", 
    "V", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V", 
    "V", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V", 
    "V", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V", 
    "V", "V", "V", "V", "V"), COND = c("NEG-CTR", "NEG-NOC", 
    "NEU-NOC", "NEG-CTR", "NEG-NOC", "NEU-NOC", "NEG-CTR", "NEG-NOC", 
    "NEU-NOC", "NEG-CTR", "NEG-NOC", "NEU-NOC", "NEG-CTR", "NEG-NOC", 
    "NEU-NOC", "NEG-CTR", "NEG-NOC", "NEU-NOC", "NEG-CTR", "NEG-NOC", 
    "NEU-NOC", "NEG-CTR", "NEG-NOC", "NEU-NOC", "NEG-CTR", "NEG-NOC", 
    "NEU-NOC", "NEG-CTR", "NEG-NOC", "NEU-NOC", "NEG-CTR", "NEG-NOC", 
    "NEU-NOC", "NEG-CTR", "NEG-NOC", "NEU-NOC", "NEG-CTR", "NEG-NOC", 
    "NEU-NOC", "NEG-CTR", "NEG-NOC", "NEU-NOC", "NEG-CTR", "NEG-NOC", 
    "NEU-NOC", "NEG-CTR", "NEG-NOC", "NEU-NOC", "NEG-CTR", "NEG-NOC"
    ), signals = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("P3FCz", 
    "P3Cz", "P3Pz", "LPPearlyFCz", "LPPearlyCz", "LPPearlyPz", 
    "LPP1FCz", "LPP1Cz", "LPP1Pz", "LPP2FCz", "LPP2Cz", "LPP2Pz", 
    "LPP2POz"), class = "factor"), value = c(-11.6312151716924, 
    -11.1438413285935, -3.99591470944713, -0.314155675382471, 
    0.238885648959708, 5.03749946898385, -0.213621915029167, 
    -2.96032491743069, -1.97168681693488, -2.83109425298642, 
    1.09291198163802, -6.692991645215, 4.23849942428043, 2.9898889629932, 
    3.5510699900835, 9.57481668808606, 5.4167795618285, 1.7067607715475, 
    -6.13036076093477, -2.82955734597919, -2.50672211111696, 
    0.528517585832501, 8.16418133488309, 1.88777321897925, -7.73588468896919, 
    -9.83058052401056, -6.97442700196932, 1.27327945355082, 2.11962397764132, 
    0.524299677616254, -1.83310726842883, 0.658810483381172, 
    -0.261373488428192, 4.37524298634374, 0.625555654900511, 
    3.19617639836154, 0.0405517582137798, -3.29357103412113, 
    -0.381435057304614, -5.73445509910268, -6.1129152355645, 
    -2.45744234877604, 2.95352732001065, 0.527721249096473, 1.91803490989119, 
    -3.46703346467546, -2.40438419043702, -5.35374408162217, 
    -7.27028665849262, -7.1532211375959)), row.names = c(NA, 
-50L), class = c("tbl_df", "tbl", "data.frame"))

and the commands lines which I fitted the model through (because these are tables showing model output)

library(lme4) 
library(dplyr)

    models_list_2 <- out_long %>%
      group_by(signals) %>%
      do(fit = lmerTest::lmer(value ~ COND + (1|ID), data = .)) %>% 
      pull(fit)

Solution

  • Using mapply in case where we would want apply but on multiple lists/vectors (i.e. one list has models and another the titles we want to use:)

    From ?mapply:

    mapply is a multivariate version of sapply. mapply applies FUN to the first elements of each ... argument, the second elements, the third elements, and so on. Arguments are recycled if necessary.

    models <- 1:4
    titles <- paste0("Desc of ", models)
    
    dummy_fun  <- function(x, y, other1, other2) return(paste0(x, y, other1, other2))
    

    We pass the two (ore more) equal length vectors as unnamed arguments, FUN takes the function we want to apply and all other (constant) arguments we want to pass to FUN go packet into a list into MoreArgs

    mapply(models, titles, FUN = dummy_fun, MoreArgs = list(other1 = "a", other2 = "b"))