如何为一系列表格分配标题

How to assign title to series of tables

我要创作一系列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)))

其中 title 输入 应该分配一个像 'Description of....' 这样的标题,然后是它所代表的适当的相关信号名称:

['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']

对于标题,我刚刚尝试创建以下 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')

即 运行 原样,第一个代号每个 table 括号中标明的名字,或 'P3(400-450).FCz'

我应该如何设置 title input 以便将报告中的每个名称分配给不同的 table 标题 object?

感谢关注

这是我正在处理的数据集

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"))

以及我用来拟合模型的命令行(因为这些 table 显示模型输出)

library(lme4) 
library(dplyr)

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

在我们想要 apply 的情况下使用 mapply 但在多个 lists/vectors 上(即一个列表有模型,另一个列表有我们想要使用的标题:)

来自?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))

我们将两个(或更多)等长向量作为未命名参数传递,FUN 采用我们要应用的函数和所有其他(常量)参数 我们想传递给 FUN go packet into a list into MoreArgs

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