How to run a command for each group in a column of a datframe?

Hi community!!!
I have a dataframe like the following:

df <- structure(list(StudyID = c("Australia", "Australia", 
                           "Australia", "Australia", "Australia", "Australia", 
                           "Australia", "Australia", "Australia", "Australia", 
                           "Australia", "Australia", "Australia", "Australia", 
                           "Australia", "Australia", "Newzealand", "Newzealand", 
                           "Newzealand", "Newzealand", "Newzealand", "Newzealand", 
                           "Newzealand", "Newzealand", "Newzealand", "Newzealand", 
                           "Newzealand", "Newzealand", "Newzealand", "Newzealand", 
                           "Newzealand", "Newzealand"), variable_control = c("Mathew", 
                                                                             "Mathew", "Mathew", "Mathew", "Simp", "Simp", "Simp", "Simp", 
                                                                             "hutten", "hutten", "hutten", "hutten", "AB", 
                                                                             "AB", "AB", "AB", "Mathew", "Mathew", "Mathew", "Mathew", "Simp", 
                                                                             "Simp", "Simp", "Simp", "hutten", "hutten", 
                                                                             "hutten", "hutten", "AB", "AB", "AB", "AB"), mean_control = c(103.125, 
                                                                                                                                           103.125, 103.125, 103.125, 3.20026104175422, 3.20026104175422, 
                                                                                                                                           3.20026104175422, 3.20026104175422, 0.655931978647405, 0.655931978647405, 
                                                                                                                                           0.655931978647405, 0.655931978647405, 5.6147916190302, 5.6147916190302, 
                                                                                                                                           5.6147916190302, 5.6147916190302, 86.3478260869565, 86.3478260869565, 
                                                                                                                                           86.3478260869565, 86.3478260869565, 2.70600451720365, 2.70600451720365, 
                                                                                                                                           2.70600451720365, 2.70600451720365, 0.776713622717005, 0.776713622717005, 
                                                                                                                                           0.776713622717005, 0.776713622717005, 2.25282841071637, 2.25282841071637, 
                                                                                                                                           2.25282841071637, 2.25282841071637), sd_control = c(14.0136837888306, 
                                                                                                                                                                                               14.0136837888306, 14.0136837888306, 14.0136837888306, 0.278705816120361, 
                                                                                                                                                                                               0.278705816120361, 0.278705816120361, 0.278705816120361, 0.0899465623045049, 
                                                                                                                                                                                               0.0899465623045049, 0.0899465623045049, 0.0899465623045049, 6.28461426690684, 
                                                                                                                                                                                               6.28461426690684, 6.28461426690684, 6.28461426690684, 22.9900629657485, 
                                                                                                                                                                                               22.9900629657485, 22.9900629657485, 22.9900629657485, 0.492994678382565, 
                                                                                                                                                                                               0.492994678382565, 0.492994678382565, 0.492994678382565, 0.129978730202719, 
                                                                                                                                                                                               0.129978730202719, 0.129978730202719, 0.129978730202719, 7.7819081791642, 
                                                                                                                                                                                               7.7819081791642, 7.7819081791642, 7.7819081791642), n_control = c(16L, 
                                                                                                                                                                                                                                                                 16L, 16L, 16L, 16L, 16L, 16L, 16L, 120L, 120L, 120L, 120L, 15L, 
                                                                                                                                                                                                                                                                 15L, 15L, 15L, 46L, 46L, 46L, 46L, 46L, 46L, 46L, 46L, 1035L, 
                                                                                                                                                                                                                                                                 1035L, 1035L, 1035L, 45L, 45L, 45L, 45L), se_control = c(3.50342094720765, 
                                                                                                                                                                                                                                                                                                                          3.50342094720765, 3.50342094720765, 3.50342094720765, 0.0696764540300902, 
                                                                                                                                                                                                                                                                                                                          0.0696764540300902, 0.0696764540300902, 0.0696764540300902, 0.00821096019070354, 
                                                                                                                                                                                                                                                                                                                          0.00821096019070354, 0.00821096019070354, 0.00821096019070354, 
                                                                                                                                                                                                                                                                                                                          1.62268042620452, 1.62268042620452, 1.62268042620452, 1.62268042620452, 
                                                                                                                                                                                                                                                                                                                          3.38969985579422, 3.38969985579422, 3.38969985579422, 3.38969985579422, 
                                                                                                                                                                                                                                                                                                                          0.0726880997546798, 0.0726880997546798, 0.0726880997546798, 0.0726880997546798, 
                                                                                                                                                                                                                                                                                                                          0.00404019302943354, 0.00404019302943354, 0.00404019302943354, 
                                                                                                                                                                                                                                                                                                                          0.00404019302943354, 1.16005837888485, 1.16005837888485, 1.16005837888485, 
                                                                                                                                                                                                                                                                                                                          1.16005837888485), lowCI_control = c(95.6576350141697, 95.6576350141697, 
                                                                                                                                                                                                                                                                                                                                                               95.6576350141697, 95.6576350141697, 3.05174919547557, 3.05174919547557, 
                                                                                                                                                                                                                                                                                                                                                               3.05174919547557, 3.05174919547557, 0.6396734573882, 0.6396734573882, 
                                                                                                                                                                                                                                                                                                                                                               0.6396734573882, 0.6396734573882, 2.13448824216196, 2.13448824216196, 
                                                                                                                                                                                                                                                                                                                                                               2.13448824216196, 2.13448824216196, 79.5206201201125, 79.5206201201125, 
                                                                                                                                                                                                                                                                                                                                                               79.5206201201125, 79.5206201201125, 2.55960316915644, 2.55960316915644, 
                                                                                                                                                                                                                                                                                                                                                               2.55960316915644, 2.55960316915644, 0.768785709935317, 0.768785709935317, 
                                                                                                                                                                                                                                                                                                                                                               0.768785709935317, 0.768785709935317, -0.0851156305499581, -0.0851156305499581, 
                                                                                                                                                                                                                                                                                                                                                               -0.0851156305499581, -0.0851156305499581), hiCI_control = c(110.59236498583, 
                                                                                                                                                                                                                                                                                                                                                                                                                           110.59236498583, 110.59236498583, 110.59236498583, 3.34877288803287, 
                                                                                                                                                                                                                                                                                                                                                                                                                           3.34877288803287, 3.34877288803287, 3.34877288803287, 0.67219049990661, 
                                                                                                                                                                                                                                                                                                                                                                                                                           0.67219049990661, 0.67219049990661, 0.67219049990661, 9.09509499589843, 
                                                                                                                                                                                                                                                                                                                                                                                                                           9.09509499589843, 9.09509499589843, 9.09509499589843, 93.1750320538006, 
                                                                                                                                                                                                                                                                                                                                                                                                                           93.1750320538006, 93.1750320538006, 93.1750320538006, 2.85240586525086, 
                                                                                                                                                                                                                                                                                                                                                                                                                           2.85240586525086, 2.85240586525086, 2.85240586525086, 0.784641535498692, 
                                                                                                                                                                                                                                                                                                                                                                                                                           0.784641535498692, 0.784641535498692, 0.784641535498692, 4.59077245198269, 
                                                                                                                                                                                                                                                                                                                                                                                                                           4.59077245198269, 4.59077245198269, 4.59077245198269), Variable_test = c("Mathew", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    "Simp", "hutten", "AB", "Mathew", "Simp", "hutten", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    "AB", "Mathew", "Simp", "hutten", "AB", "Mathew", "Simp", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    "hutten", "AB", "Mathew", "Simp", "hutten", "AB", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    "Mathew", "Simp", "hutten", "AB", "Mathew", "Simp", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    "hutten", "AB", "Mathew", "Simp", "hutten", "AB"), 
               mean_test = c(94.85, 3.11469350939242, 0.638430601983837, 
                             6.91683472226622, 94.85, 3.11469350939242, 0.638430601983837, 
                             6.91683472226622, 94.85, 3.11469350939242, 0.638430601983837, 
                             6.91683472226622, 94.85, 3.11469350939242, 0.638430601983837, 
                             6.91683472226622, 86.0222222222222, 2.6390595091832, 0.759731493048648, 
                             0.73921233900569, 86.0222222222222, 2.6390595091832, 0.759731493048648, 
                             0.73921233900569, 86.0222222222222, 2.6390595091832, 0.759731493048648, 
                             0.73921233900569, 86.0222222222222, 2.6390595091832, 0.759731493048648, 
                             0.73921233900569), sd_test = c(17.2909316055982, 0.202930255027022, 
                                                            0.0919447770686176, 8.14939209465999, 17.2909316055982, 0.202930255027022, 
                                                            0.0919447770686176, 8.14939209465999, 17.2909316055982, 0.202930255027022, 
                                                            0.0919447770686176, 8.14939209465999, 17.2909316055982, 0.202930255027022, 
                                                            0.0919447770686176, 8.14939209465999, 21.4163158796882, 0.451677388306635, 
                                                            0.138197141547272, 0.908654293596164, 21.4163158796882, 0.451677388306635, 
                                                            0.138197141547272, 0.908654293596164, 21.4163158796882, 0.451677388306635, 
                                                            0.138197141547272, 0.908654293596164, 21.4163158796882, 0.451677388306635, 
                                                            0.138197141547272, 0.908654293596164), n_test = c(20L, 20L, 
                                                                                                              190L, 21L, 20L, 20L, 190L, 21L, 20L, 20L, 190L, 21L, 20L, 
                                                                                                              20L, 190L, 21L, 45L, 45L, 990L, 45L, 45L, 45L, 990L, 45L, 
                                                                                                              45L, 45L, 990L, 45L, 45L, 45L, 990L, 45L), se_test = c(3.86636984644171, 
                                                                                                                                                                     0.0453765844931791, 0.00667037520849417, 1.77834314960258, 
                                                                                                                                                                     3.86636984644171, 0.0453765844931791, 0.00667037520849417, 
                                                                                                                                                                     1.77834314960258, 3.86636984644171, 0.0453765844931791, 0.00667037520849417, 
                                                                                                                                                                     1.77834314960258, 3.86636984644171, 0.0453765844931791, 0.00667037520849417, 
                                                                                                                                                                     1.77834314960258, 3.1925558756394, 0.0673320896102137, 0.00439219348020005, 
                                                                                                                                                                     0.135454184568538, 3.1925558756394, 0.0673320896102137, 0.00439219348020005, 
                                                                                                                                                                     0.135454184568538, 3.1925558756394, 0.0673320896102137, 0.00439219348020005, 
                                                                                                                                                                     0.135454184568538, 3.1925558756394, 0.0673320896102137, 0.00439219348020005, 
                                                                                                                                                                     0.135454184568538), lowCI_test = c(86.7575949081585, 3.01971922654131, 
                                                                                                                                                                                                        0.625272652671813, 3.20727591549959, 86.7575949081585, 3.01971922654131, 
                                                                                                                                                                                                        0.625272652671813, 3.20727591549959, 86.7575949081585, 3.01971922654131, 
                                                                                                                                                                                                        0.625272652671813, 3.20727591549959, 86.7575949081585, 3.01971922654131, 
                                                                                                                                                                                                        0.625272652671813, 3.20727591549959, 79.5880486308587, 2.50336059906323, 
                                                                                                                                                                                                        0.751112403965226, 0.466222367603537, 79.5880486308587, 2.50336059906323, 
                                                                                                                                                                                                        0.751112403965226, 0.466222367603537, 79.5880486308587, 2.50336059906323, 
                                                                                                                                                                                                        0.751112403965226, 0.466222367603537, 79.5880486308587, 2.50336059906323, 
                                                                                                                                                                                                        0.751112403965226, 0.466222367603537), hiCI_test = c(102.942405091841, 
                                                                                                                                                                                                                                                             3.20966779224354, 0.651588551295861, 10.6263935290328, 102.942405091841, 
                                                                                                                                                                                                                                                             3.20966779224354, 0.651588551295861, 10.6263935290328, 102.942405091841, 
                                                                                                                                                                                                                                                             3.20966779224354, 0.651588551295861, 10.6263935290328, 102.942405091841, 
                                                                                                                                                                                                                                                             3.20966779224354, 0.651588551295861, 10.6263935290328, 92.4563958135858, 
                                                                                                                                                                                                                                                             2.77475841930317, 0.768350582132071, 1.01220231040784, 92.4563958135858, 
                                                                                                                                                                                                                                                             2.77475841930317, 0.768350582132071, 1.01220231040784, 92.4563958135858, 
                                                                                                                                                                                                                                                             2.77475841930317, 0.768350582132071, 1.01220231040784, 92.4563958135858, 
                                                                                                                                                                                                                                                             2.77475841930317, 0.768350582132071, 1.01220231040784)), row.names = c(NA, 
                                                                                                                                                                                                                                                                                                                                    -32L), class = "data.frame")

I want to run the following command (from package metafor) on this dataframe df with a condition:

my_data <- escalc(n1i = n_control, n2i = n_test, m1i = mean_control, m2i = mean_test, 
    sd1i = sd_control, sd2i = sd_test, data = df, measure = "SMD", 
    append = TRUE)

I want to run the command 4 times for four different subsets from the dataframe (df). First, it will run on a subset dtaframe that will contain variable column value Mathew only and get an output. Next it will run on a dataframe subset that will contain variable column value AB only and get an output and so on..

Thanks in advance,
DC7

library(metafor)
#> Loading required package: Matrix
#> Loading 'metafor' package (version 2.4-0). For an overview 
#> and introduction to the package please type: help(metafor).

dat <- data.frame(
  StudyID = c(
    "Australia", "Australia",
    "Australia", "Australia", "Australia", "Australia",
    "Australia", "Australia", "Australia", "Australia",
    "Australia", "Australia", "Australia", "Australia",
    "Australia", "Australia", "Newzealand", "Newzealand",
    "Newzealand", "Newzealand", "Newzealand", "Newzealand",
    "Newzealand", "Newzealand", "Newzealand", "Newzealand",
    "Newzealand", "Newzealand", "Newzealand", "Newzealand",
    "Newzealand", "Newzealand"
  ), variable_control = c(
    "Mathew",
    "Mathew", "Mathew", "Mathew", "Simp", "Simp", "Simp", "Simp",
    "hutten", "hutten", "hutten", "hutten", "AB",
    "AB", "AB", "AB", "Mathew", "Mathew", "Mathew", "Mathew", "Simp",
    "Simp", "Simp", "Simp", "hutten", "hutten",
    "hutten", "hutten", "AB", "AB", "AB", "AB"
  ),
  mean_control = c(
    103.125,
    103.125, 103.125, 103.125, 3.20026104175422, 3.20026104175422,
    3.20026104175422, 3.20026104175422, 0.655931978647405, 0.655931978647405,
    0.655931978647405, 0.655931978647405, 5.6147916190302, 5.6147916190302,
    5.6147916190302, 5.6147916190302, 86.3478260869565, 86.3478260869565,
    86.3478260869565, 86.3478260869565, 2.70600451720365, 2.70600451720365,
    2.70600451720365, 2.70600451720365, 0.776713622717005, 0.776713622717005,
    0.776713622717005, 0.776713622717005, 2.25282841071637, 2.25282841071637,
    2.25282841071637, 2.25282841071637
  ), sd_control = c(
    14.0136837888306,
    14.0136837888306, 14.0136837888306, 14.0136837888306, 0.278705816120361,
    0.278705816120361, 0.278705816120361, 0.278705816120361, 0.0899465623045049,
    0.0899465623045049, 0.0899465623045049, 0.0899465623045049, 6.28461426690684,
    6.28461426690684, 6.28461426690684, 6.28461426690684, 22.9900629657485,
    22.9900629657485, 22.9900629657485, 22.9900629657485, 0.492994678382565,
    0.492994678382565, 0.492994678382565, 0.492994678382565, 0.129978730202719,
    0.129978730202719, 0.129978730202719, 0.129978730202719, 7.7819081791642,
    7.7819081791642, 7.7819081791642, 7.7819081791642
  ), n_control = c(
    16L,
    16L, 16L, 16L, 16L, 16L, 16L, 16L, 120L, 120L, 120L, 120L, 15L,
    15L, 15L, 15L, 46L, 46L, 46L, 46L, 46L, 46L, 46L, 46L, 1035L,
    1035L, 1035L, 1035L, 45L, 45L, 45L, 45L
  ),
  se_control = c(
    3.50342094720765,
    3.50342094720765, 3.50342094720765, 3.50342094720765, 0.0696764540300902,
    0.0696764540300902, 0.0696764540300902, 0.0696764540300902, 0.00821096019070354, 0.00821096019070354, 0.00821096019070354, 0.00821096019070354,
    1.62268042620452, 1.62268042620452, 1.62268042620452, 1.62268042620452,
    3.38969985579422, 3.38969985579422, 3.38969985579422, 3.38969985579422,
    0.0726880997546798, 0.0726880997546798, 0.0726880997546798, 0.0726880997546798,
    0.00404019302943354, 0.00404019302943354, 0.00404019302943354,
    0.00404019302943354, 1.16005837888485, 1.16005837888485, 1.16005837888485,
    1.16005837888485
  ),
  lowCI_control = c(
    95.6576350141697, 95.6576350141697,
    95.6576350141697, 95.6576350141697, 3.05174919547557, 3.05174919547557,
    3.05174919547557, 3.05174919547557, 0.6396734573882, 0.6396734573882,
    0.6396734573882, 0.6396734573882, 2.13448824216196, 2.13448824216196,
    2.13448824216196, 2.13448824216196, 79.5206201201125, 79.5206201201125,
    79.5206201201125, 79.5206201201125, 2.55960316915644, 2.55960316915644,
    2.55960316915644, 2.55960316915644, 0.768785709935317, 0.768785709935317,
    0.768785709935317, 0.768785709935317, -0.0851156305499581, -0.0851156305499581,
    -0.0851156305499581, -0.0851156305499581
  ), hiCI_control = c(
    110.59236498583,
    110.59236498583, 110.59236498583, 110.59236498583, 3.34877288803287,
    3.34877288803287, 3.34877288803287, 3.34877288803287, 0.67219049990661,
    0.67219049990661, 0.67219049990661, 0.67219049990661, 9.09509499589843,
    9.09509499589843, 9.09509499589843, 9.09509499589843, 93.1750320538006,
    93.1750320538006, 93.1750320538006, 93.1750320538006, 2.85240586525086,
    2.85240586525086, 2.85240586525086, 2.85240586525086, 0.784641535498692,
    0.784641535498692, 0.784641535498692, 0.784641535498692, 4.59077245198269,
    4.59077245198269, 4.59077245198269, 4.59077245198269
  ), Variable_test = c(
    "Mathew",
    "Simp", "hutten", "AB", "Mathew", "Simp", "hutten",
    "AB", "Mathew", "Simp", "hutten", "AB", "Mathew", "Simp",
    "hutten", "AB", "Mathew", "Simp", "hutten", "AB",
    "Mathew", "Simp", "hutten", "AB", "Mathew", "Simp",
    "hutten", "AB", "Mathew", "Simp", "hutten", "AB"
  ),
  mean_test = c(
    94.85, 3.11469350939242, 0.638430601983837,
    6.91683472226622, 94.85, 3.11469350939242, 0.638430601983837,
    6.91683472226622, 94.85, 3.11469350939242, 0.638430601983837,
    6.91683472226622, 94.85, 3.11469350939242, 0.638430601983837,
    6.91683472226622, 86.0222222222222, 2.6390595091832, 0.759731493048648,
    0.73921233900569, 86.0222222222222, 2.6390595091832, 0.759731493048648,
    0.73921233900569, 86.0222222222222, 2.6390595091832, 0.759731493048648,
    0.73921233900569, 86.0222222222222, 2.6390595091832, 0.759731493048648,
    0.73921233900569
  ),
  sd_test = c(
    17.2909316055982, 0.202930255027022,
    0.0919447770686176, 8.14939209465999, 17.2909316055982, 0.202930255027022,
    0.0919447770686176, 8.14939209465999, 17.2909316055982, 0.202930255027022,
    0.0919447770686176, 8.14939209465999, 17.2909316055982, 0.202930255027022,
    0.0919447770686176, 8.14939209465999, 21.4163158796882, 0.451677388306635,
    0.138197141547272, 0.908654293596164, 21.4163158796882, 0.451677388306635,
    0.138197141547272, 0.908654293596164, 21.4163158796882, 0.451677388306635,
    0.138197141547272, 0.908654293596164, 21.4163158796882, 0.451677388306635,
    0.138197141547272, 0.908654293596164
  ),
  n_test = c(
    20L, 20L,
    190L, 21L, 20L, 20L, 190L, 21L, 20L, 20L, 190L, 21L, 20L,
    20L, 190L, 21L, 45L, 45L, 990L, 45L, 45L, 45L, 990L, 45L,
    45L, 45L, 990L, 45L, 45L, 45L, 990L, 45L
  ),
  se_test = c(
    3.86636984644171,
    0.0453765844931791, 0.00667037520849417, 1.77834314960258,
    3.86636984644171, 0.0453765844931791, 0.00667037520849417,
    1.77834314960258, 3.86636984644171, 0.0453765844931791, 0.00667037520849417,
    1.77834314960258, 3.86636984644171, 0.0453765844931791, 0.00667037520849417,
    1.77834314960258, 3.1925558756394, 0.0673320896102137, 0.00439219348020005,
    0.135454184568538, 3.1925558756394, 0.0673320896102137, 0.00439219348020005,
    0.135454184568538, 3.1925558756394, 0.0673320896102137, 0.00439219348020005,
    0.135454184568538, 3.1925558756394, 0.0673320896102137, 0.00439219348020005,
    0.135454184568538
  ), lowCI_test = c(
    86.7575949081585, 3.01971922654131,
    0.625272652671813, 3.20727591549959, 86.7575949081585, 3.01971922654131,
    0.625272652671813, 3.20727591549959, 86.7575949081585, 3.01971922654131,
    0.625272652671813, 3.20727591549959, 86.7575949081585, 3.01971922654131,
    0.625272652671813, 3.20727591549959, 79.5880486308587, 2.50336059906323,
    0.751112403965226, 0.466222367603537, 79.5880486308587, 2.50336059906323,
    0.751112403965226, 0.466222367603537, 79.5880486308587, 2.50336059906323,
    0.751112403965226, 0.466222367603537, 79.5880486308587, 2.50336059906323,
    0.751112403965226, 0.466222367603537
  ),
  hiCI_test = c(
    102.942405091841,
    3.20966779224354, 0.651588551295861, 10.6263935290328, 102.942405091841,
    3.20966779224354, 0.651588551295861, 10.6263935290328, 102.942405091841,
    3.20966779224354, 0.651588551295861, 10.6263935290328, 102.942405091841,
    3.20966779224354, 0.651588551295861, 10.6263935290328, 92.4563958135858,
    2.77475841930317, 0.768350582132071, 1.01220231040784, 92.4563958135858,
    2.77475841930317, 0.768350582132071, 1.01220231040784, 92.4563958135858,
    2.77475841930317, 0.768350582132071, 1.01220231040784, 92.4563958135858,
    2.77475841930317, 0.768350582132071, 1.01220231040784
  )
)

run_escalc <- function(x){
  escalc(n1i = n_control, 
         n2i = n_test, 
         m1i = mean_control, 
         m2i = mean_test,
         sd1i = sd_control, 
         sd2i = sd_test, 
         data = subset(dat,variable_control == x), 
         measure = "SMD",
         append = TRUE)
}

the_vars <- unique(dat[,"variable_control"])

Map(run_escalc,the_vars)
#> $Mathew
#>       StudyID variable_control mean_control sd_control n_control se_control 
#> 1   Australia           Mathew    103.12500   14.01368        16   3.503421 
#> 2   Australia           Mathew    103.12500   14.01368        16   3.503421 
#> 3   Australia           Mathew    103.12500   14.01368        16   3.503421 
#> 4   Australia           Mathew    103.12500   14.01368        16   3.503421 
#> 17 Newzealand           Mathew     86.34783   22.99006        46   3.389700 
#> 18 Newzealand           Mathew     86.34783   22.99006        46   3.389700 
#> 19 Newzealand           Mathew     86.34783   22.99006        46   3.389700 
#> 20 Newzealand           Mathew     86.34783   22.99006        46   3.389700 
#>    lowCI_control hiCI_control Variable_test  mean_test     sd_test n_test 
#> 1       95.65764    110.59236        Mathew 94.8500000 17.29093161     20 
#> 2       95.65764    110.59236          Simp  3.1146935  0.20293026     20 
#> 3       95.65764    110.59236        hutten  0.6384306  0.09194478    190 
#> 4       95.65764    110.59236            AB  6.9168347  8.14939209     21 
#> 17      79.52062     93.17503        Mathew 86.0222222 21.41631588     45 
#> 18      79.52062     93.17503          Simp  2.6390595  0.45167739     45 
#> 19      79.52062     93.17503        hutten  0.7597315  0.13819714    990 
#> 20      79.52062     93.17503            AB  0.7392123  0.90865429     45 
#>        se_test lowCI_test   hiCI_test      yi     vi 
#> 1  3.866369846 86.7575949 102.9424051  0.5080 0.1161 
#> 2  0.045376584  3.0197192   3.2096678 10.5040 1.6449 
#> 3  0.006670375  0.6252727   0.6515886 26.8636 1.8193 
#> 4  1.778343150  3.2072759  10.6263935  8.5181 1.0906 
#> 17 3.192555876 79.5880486  92.4563958  0.0145 0.0440 
#> 18 0.067332090  2.5033606   2.7747584  5.0763 0.1856 
#> 19 0.004392193  0.7511124   0.7683506 17.8254 0.1761 
#> 20 0.135454185  0.4662224   1.0122023  5.1886 0.1919 
#> 
#> $Simp
#>       StudyID variable_control mean_control sd_control n_control se_control 
#> 5   Australia             Simp     3.200261  0.2787058        16 0.06967645 
#> 6   Australia             Simp     3.200261  0.2787058        16 0.06967645 
#> 7   Australia             Simp     3.200261  0.2787058        16 0.06967645 
#> 8   Australia             Simp     3.200261  0.2787058        16 0.06967645 
#> 21 Newzealand             Simp     2.706005  0.4929947        46 0.07268810 
#> 22 Newzealand             Simp     2.706005  0.4929947        46 0.07268810 
#> 23 Newzealand             Simp     2.706005  0.4929947        46 0.07268810 
#> 24 Newzealand             Simp     2.706005  0.4929947        46 0.07268810 
#>    lowCI_control hiCI_control Variable_test  mean_test     sd_test n_test 
#> 5       3.051749     3.348773        Mathew 94.8500000 17.29093161     20 
#> 6       3.051749     3.348773          Simp  3.1146935  0.20293026     20 
#> 7       3.051749     3.348773        hutten  0.6384306  0.09194478    190 
#> 8       3.051749     3.348773            AB  6.9168347  8.14939209     21 
#> 21      2.559603     2.852406        Mathew 86.0222222 21.41631588     45 
#> 22      2.559603     2.852406          Simp  2.6390595  0.45167739     45 
#> 23      2.559603     2.852406        hutten  0.7597315  0.13819714    990 
#> 24      2.559603     2.852406            AB  0.7392123  0.90865429     45 
#>        se_test lowCI_test   hiCI_test      yi     vi 
#> 5  3.866369846 86.7575949 102.9424051 -6.9320 0.7799 
#> 6  0.045376584  3.0197192   3.2096678  0.3496 0.1142 
#> 7  0.006670375  0.6252727   0.6515886 21.9320 1.2353 
#> 8  1.778343150  3.2072759  10.6263935 -0.5900 0.1148 
#> 21 3.192555876 79.5880486  92.4563958 -5.4846 0.2092 
#> 22 0.067332090  2.5033606   2.7747584  0.1403 0.0441 
#> 23 0.004392193  0.7511124   0.7683506 11.4513 0.0860 
#> 24 0.135454185  0.4662224   1.0122023  2.6760 0.0833 
#> 
#> $hutten
#>       StudyID variable_control mean_control sd_control n_control  se_control 
#> 9   Australia           hutten    0.6559320 0.08994656       120 0.008210960 
#> 10  Australia           hutten    0.6559320 0.08994656       120 0.008210960 
#> 11  Australia           hutten    0.6559320 0.08994656       120 0.008210960 
#> 12  Australia           hutten    0.6559320 0.08994656       120 0.008210960 
#> 25 Newzealand           hutten    0.7767136 0.12997873      1035 0.004040193 
#> 26 Newzealand           hutten    0.7767136 0.12997873      1035 0.004040193 
#> 27 Newzealand           hutten    0.7767136 0.12997873      1035 0.004040193 
#> 28 Newzealand           hutten    0.7767136 0.12997873      1035 0.004040193 
#>    lowCI_control hiCI_control Variable_test  mean_test     sd_test n_test 
#> 9      0.6396735    0.6721905        Mathew 94.8500000 17.29093161     20 
#> 10     0.6396735    0.6721905          Simp  3.1146935  0.20293026     20 
#> 11     0.6396735    0.6721905        hutten  0.6384306  0.09194478    190 
#> 12     0.6396735    0.6721905            AB  6.9168347  8.14939209     21 
#> 25     0.7687857    0.7846415        Mathew 86.0222222 21.41631588     45 
#> 26     0.7687857    0.7846415          Simp  2.6390595  0.45167739     45 
#> 27     0.7687857    0.7846415        hutten  0.7597315  0.13819714    990 
#> 28     0.7687857    0.7846415            AB  0.7392123  0.90865429     45 
#>        se_test lowCI_test   hiCI_test       yi     vi 
#> 9  3.866369846 86.7575949 102.9424051 -14.6002 0.8196 
#> 10 0.045376584  3.0197192   3.2096678 -21.7452 1.7471 
#> 11 0.006670375  0.6252727   0.6515886   0.1915 0.0137 
#> 12 1.778343150  3.2072759  10.6263935  -2.0137 0.0703 
#> 25 3.192555876 79.5880486  92.4563958 -19.6797 0.2025 
#> 26 0.067332090  2.5033606   2.7747584 -11.8821 0.0886 
#> 27 0.004392193  0.7511124   0.7683506   0.1266 0.0020 
#> 28 0.135454185  0.4662224   1.0122023   0.1678 0.0232 
#> 
#> $AB
#>       StudyID variable_control mean_control sd_control n_control se_control 
#> 13  Australia               AB     5.614792   6.284614        15   1.622680 
#> 14  Australia               AB     5.614792   6.284614        15   1.622680 
#> 15  Australia               AB     5.614792   6.284614        15   1.622680 
#> 16  Australia               AB     5.614792   6.284614        15   1.622680 
#> 29 Newzealand               AB     2.252828   7.781908        45   1.160058 
#> 30 Newzealand               AB     2.252828   7.781908        45   1.160058 
#> 31 Newzealand               AB     2.252828   7.781908        45   1.160058 
#> 32 Newzealand               AB     2.252828   7.781908        45   1.160058 
#>    lowCI_control hiCI_control Variable_test  mean_test     sd_test n_test 
#> 13    2.13448824     9.095095        Mathew 94.8500000 17.29093161     20 
#> 14    2.13448824     9.095095          Simp  3.1146935  0.20293026     20 
#> 15    2.13448824     9.095095        hutten  0.6384306  0.09194478    190 
#> 16    2.13448824     9.095095            AB  6.9168347  8.14939209     21 
#> 29   -0.08511563     4.590772        Mathew 86.0222222 21.41631588     45 
#> 30   -0.08511563     4.590772          Simp  2.6390595  0.45167739     45 
#> 31   -0.08511563     4.590772        hutten  0.7597315  0.13819714    990 
#> 32   -0.08511563     4.590772            AB  0.7392123  0.90865429     45 
#>        se_test lowCI_test   hiCI_test      yi     vi 
#> 13 3.866369846 86.7575949 102.9424051 -6.3438 0.6916 
#> 14 0.045376584  3.0197192   3.2096678  0.5963 0.1217 
#> 15 0.006670375  0.6252727   0.6515886  2.9997 0.0939 
#> 16 1.778343150  3.2072759  10.6263935 -0.1711 0.1147 
#> 29 3.192555876 79.5880486  92.4563958 -5.1546 0.1921 
#> 30 0.067332090  2.5033606   2.7747584 -0.0695 0.0445 
#> 31 0.004392193  0.7511124   0.7683506  0.9257 0.0236 
#> 32 0.135454185  0.4662224   1.0122023  0.2709 0.0449
1 Like

Thanks a lot @technocrat . can I actually get separate dataframe outputs instead of getting a list?

1 Like

Thanks for the question, it's something I've been meaning to nail down.

library(metafor)
#> Loading required package: Matrix
#> Loading 'metafor' package (version 2.4-0). For an overview 
#> and introduction to the package please type: help(metafor).

dat <- data.frame(
  StudyID = c(
    "Australia", "Australia",
    "Australia", "Australia", "Australia", "Australia",
    "Australia", "Australia", "Australia", "Australia",
    "Australia", "Australia", "Australia", "Australia",
    "Australia", "Australia", "Newzealand", "Newzealand",
    "Newzealand", "Newzealand", "Newzealand", "Newzealand",
    "Newzealand", "Newzealand", "Newzealand", "Newzealand",
    "Newzealand", "Newzealand", "Newzealand", "Newzealand",
    "Newzealand", "Newzealand"
  ), variable_control = c(
    "Mathew",
    "Mathew", "Mathew", "Mathew", "Simp", "Simp", "Simp", "Simp",
    "hutten", "hutten", "hutten", "hutten", "AB",
    "AB", "AB", "AB", "Mathew", "Mathew", "Mathew", "Mathew", "Simp",
    "Simp", "Simp", "Simp", "hutten", "hutten",
    "hutten", "hutten", "AB", "AB", "AB", "AB"
  ),
  mean_control = c(
    103.125,
    103.125, 103.125, 103.125, 3.20026104175422, 3.20026104175422,
    3.20026104175422, 3.20026104175422, 0.655931978647405, 0.655931978647405,
    0.655931978647405, 0.655931978647405, 5.6147916190302, 5.6147916190302,
    5.6147916190302, 5.6147916190302, 86.3478260869565, 86.3478260869565,
    86.3478260869565, 86.3478260869565, 2.70600451720365, 2.70600451720365,
    2.70600451720365, 2.70600451720365, 0.776713622717005, 0.776713622717005,
    0.776713622717005, 0.776713622717005, 2.25282841071637, 2.25282841071637,
    2.25282841071637, 2.25282841071637
  ), sd_control = c(
    14.0136837888306,
    14.0136837888306, 14.0136837888306, 14.0136837888306, 0.278705816120361,
    0.278705816120361, 0.278705816120361, 0.278705816120361, 0.0899465623045049,
    0.0899465623045049, 0.0899465623045049, 0.0899465623045049, 6.28461426690684,
    6.28461426690684, 6.28461426690684, 6.28461426690684, 22.9900629657485,
    22.9900629657485, 22.9900629657485, 22.9900629657485, 0.492994678382565,
    0.492994678382565, 0.492994678382565, 0.492994678382565, 0.129978730202719,
    0.129978730202719, 0.129978730202719, 0.129978730202719, 7.7819081791642,
    7.7819081791642, 7.7819081791642, 7.7819081791642
  ), n_control = c(
    16L,
    16L, 16L, 16L, 16L, 16L, 16L, 16L, 120L, 120L, 120L, 120L, 15L,
    15L, 15L, 15L, 46L, 46L, 46L, 46L, 46L, 46L, 46L, 46L, 1035L,
    1035L, 1035L, 1035L, 45L, 45L, 45L, 45L
  ),
  se_control = c(
    3.50342094720765,
    3.50342094720765, 3.50342094720765, 3.50342094720765, 0.0696764540300902,
    0.0696764540300902, 0.0696764540300902, 0.0696764540300902, 0.00821096019070354, 0.00821096019070354, 0.00821096019070354, 0.00821096019070354,
    1.62268042620452, 1.62268042620452, 1.62268042620452, 1.62268042620452,
    3.38969985579422, 3.38969985579422, 3.38969985579422, 3.38969985579422,
    0.0726880997546798, 0.0726880997546798, 0.0726880997546798, 0.0726880997546798,
    0.00404019302943354, 0.00404019302943354, 0.00404019302943354,
    0.00404019302943354, 1.16005837888485, 1.16005837888485, 1.16005837888485,
    1.16005837888485
  ),
  lowCI_control = c(
    95.6576350141697, 95.6576350141697,
    95.6576350141697, 95.6576350141697, 3.05174919547557, 3.05174919547557,
    3.05174919547557, 3.05174919547557, 0.6396734573882, 0.6396734573882,
    0.6396734573882, 0.6396734573882, 2.13448824216196, 2.13448824216196,
    2.13448824216196, 2.13448824216196, 79.5206201201125, 79.5206201201125,
    79.5206201201125, 79.5206201201125, 2.55960316915644, 2.55960316915644,
    2.55960316915644, 2.55960316915644, 0.768785709935317, 0.768785709935317,
    0.768785709935317, 0.768785709935317, -0.0851156305499581, -0.0851156305499581,
    -0.0851156305499581, -0.0851156305499581
  ), hiCI_control = c(
    110.59236498583,
    110.59236498583, 110.59236498583, 110.59236498583, 3.34877288803287,
    3.34877288803287, 3.34877288803287, 3.34877288803287, 0.67219049990661,
    0.67219049990661, 0.67219049990661, 0.67219049990661, 9.09509499589843,
    9.09509499589843, 9.09509499589843, 9.09509499589843, 93.1750320538006,
    93.1750320538006, 93.1750320538006, 93.1750320538006, 2.85240586525086,
    2.85240586525086, 2.85240586525086, 2.85240586525086, 0.784641535498692,
    0.784641535498692, 0.784641535498692, 0.784641535498692, 4.59077245198269,
    4.59077245198269, 4.59077245198269, 4.59077245198269
  ), Variable_test = c(
    "Mathew",
    "Simp", "hutten", "AB", "Mathew", "Simp", "hutten",
    "AB", "Mathew", "Simp", "hutten", "AB", "Mathew", "Simp",
    "hutten", "AB", "Mathew", "Simp", "hutten", "AB",
    "Mathew", "Simp", "hutten", "AB", "Mathew", "Simp",
    "hutten", "AB", "Mathew", "Simp", "hutten", "AB"
  ),
  mean_test = c(
    94.85, 3.11469350939242, 0.638430601983837,
    6.91683472226622, 94.85, 3.11469350939242, 0.638430601983837,
    6.91683472226622, 94.85, 3.11469350939242, 0.638430601983837,
    6.91683472226622, 94.85, 3.11469350939242, 0.638430601983837,
    6.91683472226622, 86.0222222222222, 2.6390595091832, 0.759731493048648,
    0.73921233900569, 86.0222222222222, 2.6390595091832, 0.759731493048648,
    0.73921233900569, 86.0222222222222, 2.6390595091832, 0.759731493048648,
    0.73921233900569, 86.0222222222222, 2.6390595091832, 0.759731493048648,
    0.73921233900569
  ),
  sd_test = c(
    17.2909316055982, 0.202930255027022,
    0.0919447770686176, 8.14939209465999, 17.2909316055982, 0.202930255027022,
    0.0919447770686176, 8.14939209465999, 17.2909316055982, 0.202930255027022,
    0.0919447770686176, 8.14939209465999, 17.2909316055982, 0.202930255027022,
    0.0919447770686176, 8.14939209465999, 21.4163158796882, 0.451677388306635,
    0.138197141547272, 0.908654293596164, 21.4163158796882, 0.451677388306635,
    0.138197141547272, 0.908654293596164, 21.4163158796882, 0.451677388306635,
    0.138197141547272, 0.908654293596164, 21.4163158796882, 0.451677388306635,
    0.138197141547272, 0.908654293596164
  ),
  n_test = c(
    20L, 20L,
    190L, 21L, 20L, 20L, 190L, 21L, 20L, 20L, 190L, 21L, 20L,
    20L, 190L, 21L, 45L, 45L, 990L, 45L, 45L, 45L, 990L, 45L,
    45L, 45L, 990L, 45L, 45L, 45L, 990L, 45L
  ),
  se_test = c(
    3.86636984644171,
    0.0453765844931791, 0.00667037520849417, 1.77834314960258,
    3.86636984644171, 0.0453765844931791, 0.00667037520849417,
    1.77834314960258, 3.86636984644171, 0.0453765844931791, 0.00667037520849417,
    1.77834314960258, 3.86636984644171, 0.0453765844931791, 0.00667037520849417,
    1.77834314960258, 3.1925558756394, 0.0673320896102137, 0.00439219348020005,
    0.135454184568538, 3.1925558756394, 0.0673320896102137, 0.00439219348020005,
    0.135454184568538, 3.1925558756394, 0.0673320896102137, 0.00439219348020005,
    0.135454184568538, 3.1925558756394, 0.0673320896102137, 0.00439219348020005,
    0.135454184568538
  ), lowCI_test = c(
    86.7575949081585, 3.01971922654131,
    0.625272652671813, 3.20727591549959, 86.7575949081585, 3.01971922654131,
    0.625272652671813, 3.20727591549959, 86.7575949081585, 3.01971922654131,
    0.625272652671813, 3.20727591549959, 86.7575949081585, 3.01971922654131,
    0.625272652671813, 3.20727591549959, 79.5880486308587, 2.50336059906323,
    0.751112403965226, 0.466222367603537, 79.5880486308587, 2.50336059906323,
    0.751112403965226, 0.466222367603537, 79.5880486308587, 2.50336059906323,
    0.751112403965226, 0.466222367603537, 79.5880486308587, 2.50336059906323,
    0.751112403965226, 0.466222367603537
  ),
  hiCI_test = c(
    102.942405091841,
    3.20966779224354, 0.651588551295861, 10.6263935290328, 102.942405091841,
    3.20966779224354, 0.651588551295861, 10.6263935290328, 102.942405091841,
    3.20966779224354, 0.651588551295861, 10.6263935290328, 102.942405091841,
    3.20966779224354, 0.651588551295861, 10.6263935290328, 92.4563958135858,
    2.77475841930317, 0.768350582132071, 1.01220231040784, 92.4563958135858,
    2.77475841930317, 0.768350582132071, 1.01220231040784, 92.4563958135858,
    2.77475841930317, 0.768350582132071, 1.01220231040784, 92.4563958135858,
    2.77475841930317, 0.768350582132071, 1.01220231040784
  )
)

run_escalc <- function(x){
  escalc(n1i = n_control, 
         n2i = n_test, 
         m1i = mean_control, 
         m2i = mean_test,
         sd1i = sd_control, 
         sd2i = sd_test, 
         data = subset(dat,variable_control == x), 
         measure = "SMD",
         append = TRUE)
}

the_vars <- unique(dat[,"variable_control"])

make_frame <- function(x) assign(x,data.frame(Map(run_escalc,x)[[x]]), envir = .GlobalEnv)

# none of the_vars are in the environment

ls()[(ls() %in% the_vars)]
#> character(0)

invisible(Map(make_frame,the_vars))

# the elements of the_vars are in the environment

ls()[(ls() %in% the_vars)]
#> [1] "AB"     "hutten" "Mathew" "Simp"

# example to show that the extracted element is a data frame

class(AB)
#> [1] "data.frame"
1 Like

Thanks a lot, @technocrat for your help. Your answers were immensely helpful. However, I have a follow up question. I have stored in the list (my_data) as you have done at first. Ater that, I have run the following command on the elements of the list as follows:

myfunction <- function(df) {
  rma(yi, vi, data = df)
}

x <-lapply(my_data, myfunction)

Now, I have got an output list (x) from the last step. I want to run following command on the elements of this list:
forest(ma_model_1, slab = paste(my_data$study))
Here, ma_model_1 is an element from the list x. And here, my_data$study is StudyID column from the respective element of list my_data from which an element of list x comes.

I have tried like following but did not work:

my_function_2 <- function(var){
forest(var1, slab=my_list$var1$StudyID)
}

v <- lapply(x, myfunction_2)

thanks,
DC7

going back to leaving the data in a named list, then, and applying a function to create a new list, the question is now how to apply another function to this second list. It appears that the second list should contain

either an object of class "rma", a vector with the observed effect size or outcomes, or an object of class "cumul.rma".

but I have no visibility into it. Is var1 an element? What's the roadmap to line up var1 with StudyID?

1 Like

Ok @technocrat . Give a name my_list to the first list you have created. Then do the following lines of codes to get the second list named x

myfunction <- function(df) {
  rma(yi, vi, data = df)
}

x <-lapply(my_list, myfunction)

Now, I have to run the following command on each dataframe of the second list x.
forest(x$Chao1, slab = paste(my_list$Chao1$StudyID))

I want to get the first Chao1 from list x but the second Chao1 from the second list my_list. It works the way I have written here for a single dataframe, but I am unable to run it in different dataframes in a list with lapply.
Sir, is it clear now? If not please let me know.

Thank you for your continuous support.

DC7

Short: this can be done to display each forest plot, but saving them programmatically will be difficult.

library(metafor)
#> Loading required package: Matrix
#> Loading 'metafor' package (version 2.4-0). For an overview 
#> and introduction to the package please type: help(metafor).
#> Loading required package: Matrix
#> Loading 'metafor' package (version 2.4-0). For an overview 
#> and introduction to the package please type: help(metafor).

dat <- data.frame(
  StudyID = c(
    "Australia", "Australia",
    "Australia", "Australia", "Australia", "Australia",
    "Australia", "Australia", "Australia", "Australia",
    "Australia", "Australia", "Australia", "Australia",
    "Australia", "Australia", "Newzealand", "Newzealand",
    "Newzealand", "Newzealand", "Newzealand", "Newzealand",
    "Newzealand", "Newzealand", "Newzealand", "Newzealand",
    "Newzealand", "Newzealand", "Newzealand", "Newzealand",
    "Newzealand", "Newzealand"
  ), variable_control = c(
    "Mathew",
    "Mathew", "Mathew", "Mathew", "Simp", "Simp", "Simp", "Simp",
    "hutten", "hutten", "hutten", "hutten", "AB",
    "AB", "AB", "AB", "Mathew", "Mathew", "Mathew", "Mathew", "Simp",
    "Simp", "Simp", "Simp", "hutten", "hutten",
    "hutten", "hutten", "AB", "AB", "AB", "AB"
  ),
  mean_control = c(
    103.125,
    103.125, 103.125, 103.125, 3.20026104175422, 3.20026104175422,
    3.20026104175422, 3.20026104175422, 0.655931978647405, 0.655931978647405,
    0.655931978647405, 0.655931978647405, 5.6147916190302, 5.6147916190302,
    5.6147916190302, 5.6147916190302, 86.3478260869565, 86.3478260869565,
    86.3478260869565, 86.3478260869565, 2.70600451720365, 2.70600451720365,
    2.70600451720365, 2.70600451720365, 0.776713622717005, 0.776713622717005,
    0.776713622717005, 0.776713622717005, 2.25282841071637, 2.25282841071637,
    2.25282841071637, 2.25282841071637
  ), sd_control = c(
    14.0136837888306,
    14.0136837888306, 14.0136837888306, 14.0136837888306, 0.278705816120361,
    0.278705816120361, 0.278705816120361, 0.278705816120361, 0.0899465623045049,
    0.0899465623045049, 0.0899465623045049, 0.0899465623045049, 6.28461426690684,
    6.28461426690684, 6.28461426690684, 6.28461426690684, 22.9900629657485,
    22.9900629657485, 22.9900629657485, 22.9900629657485, 0.492994678382565,
    0.492994678382565, 0.492994678382565, 0.492994678382565, 0.129978730202719,
    0.129978730202719, 0.129978730202719, 0.129978730202719, 7.7819081791642,
    7.7819081791642, 7.7819081791642, 7.7819081791642
  ), n_control = c(
    16L,
    16L, 16L, 16L, 16L, 16L, 16L, 16L, 120L, 120L, 120L, 120L, 15L,
    15L, 15L, 15L, 46L, 46L, 46L, 46L, 46L, 46L, 46L, 46L, 1035L,
    1035L, 1035L, 1035L, 45L, 45L, 45L, 45L
  ),
  se_control = c(
    3.50342094720765,
    3.50342094720765, 3.50342094720765, 3.50342094720765, 0.0696764540300902,
    0.0696764540300902, 0.0696764540300902, 0.0696764540300902, 0.00821096019070354, 0.00821096019070354, 0.00821096019070354, 0.00821096019070354,
    1.62268042620452, 1.62268042620452, 1.62268042620452, 1.62268042620452,
    3.38969985579422, 3.38969985579422, 3.38969985579422, 3.38969985579422,
    0.0726880997546798, 0.0726880997546798, 0.0726880997546798, 0.0726880997546798,
    0.00404019302943354, 0.00404019302943354, 0.00404019302943354,
    0.00404019302943354, 1.16005837888485, 1.16005837888485, 1.16005837888485,
    1.16005837888485
  ),
  lowCI_control = c(
    95.6576350141697, 95.6576350141697,
    95.6576350141697, 95.6576350141697, 3.05174919547557, 3.05174919547557,
    3.05174919547557, 3.05174919547557, 0.6396734573882, 0.6396734573882,
    0.6396734573882, 0.6396734573882, 2.13448824216196, 2.13448824216196,
    2.13448824216196, 2.13448824216196, 79.5206201201125, 79.5206201201125,
    79.5206201201125, 79.5206201201125, 2.55960316915644, 2.55960316915644,
    2.55960316915644, 2.55960316915644, 0.768785709935317, 0.768785709935317,
    0.768785709935317, 0.768785709935317, -0.0851156305499581, -0.0851156305499581,
    -0.0851156305499581, -0.0851156305499581
  ), hiCI_control = c(
    110.59236498583,
    110.59236498583, 110.59236498583, 110.59236498583, 3.34877288803287,
    3.34877288803287, 3.34877288803287, 3.34877288803287, 0.67219049990661,
    0.67219049990661, 0.67219049990661, 0.67219049990661, 9.09509499589843,
    9.09509499589843, 9.09509499589843, 9.09509499589843, 93.1750320538006,
    93.1750320538006, 93.1750320538006, 93.1750320538006, 2.85240586525086,
    2.85240586525086, 2.85240586525086, 2.85240586525086, 0.784641535498692,
    0.784641535498692, 0.784641535498692, 0.784641535498692, 4.59077245198269,
    4.59077245198269, 4.59077245198269, 4.59077245198269
  ), Variable_test = c(
    "Mathew",
    "Simp", "hutten", "AB", "Mathew", "Simp", "hutten",
    "AB", "Mathew", "Simp", "hutten", "AB", "Mathew", "Simp",
    "hutten", "AB", "Mathew", "Simp", "hutten", "AB",
    "Mathew", "Simp", "hutten", "AB", "Mathew", "Simp",
    "hutten", "AB", "Mathew", "Simp", "hutten", "AB"
  ),
  mean_test = c(
    94.85, 3.11469350939242, 0.638430601983837,
    6.91683472226622, 94.85, 3.11469350939242, 0.638430601983837,
    6.91683472226622, 94.85, 3.11469350939242, 0.638430601983837,
    6.91683472226622, 94.85, 3.11469350939242, 0.638430601983837,
    6.91683472226622, 86.0222222222222, 2.6390595091832, 0.759731493048648,
    0.73921233900569, 86.0222222222222, 2.6390595091832, 0.759731493048648,
    0.73921233900569, 86.0222222222222, 2.6390595091832, 0.759731493048648,
    0.73921233900569, 86.0222222222222, 2.6390595091832, 0.759731493048648,
    0.73921233900569
  ),
  sd_test = c(
    17.2909316055982, 0.202930255027022,
    0.0919447770686176, 8.14939209465999, 17.2909316055982, 0.202930255027022,
    0.0919447770686176, 8.14939209465999, 17.2909316055982, 0.202930255027022,
    0.0919447770686176, 8.14939209465999, 17.2909316055982, 0.202930255027022,
    0.0919447770686176, 8.14939209465999, 21.4163158796882, 0.451677388306635,
    0.138197141547272, 0.908654293596164, 21.4163158796882, 0.451677388306635,
    0.138197141547272, 0.908654293596164, 21.4163158796882, 0.451677388306635,
    0.138197141547272, 0.908654293596164, 21.4163158796882, 0.451677388306635,
    0.138197141547272, 0.908654293596164
  ),
  n_test = c(
    20L, 20L,
    190L, 21L, 20L, 20L, 190L, 21L, 20L, 20L, 190L, 21L, 20L,
    20L, 190L, 21L, 45L, 45L, 990L, 45L, 45L, 45L, 990L, 45L,
    45L, 45L, 990L, 45L, 45L, 45L, 990L, 45L
  ),
  se_test = c(
    3.86636984644171,
    0.0453765844931791, 0.00667037520849417, 1.77834314960258,
    3.86636984644171, 0.0453765844931791, 0.00667037520849417,
    1.77834314960258, 3.86636984644171, 0.0453765844931791, 0.00667037520849417,
    1.77834314960258, 3.86636984644171, 0.0453765844931791, 0.00667037520849417,
    1.77834314960258, 3.1925558756394, 0.0673320896102137, 0.00439219348020005,
    0.135454184568538, 3.1925558756394, 0.0673320896102137, 0.00439219348020005,
    0.135454184568538, 3.1925558756394, 0.0673320896102137, 0.00439219348020005,
    0.135454184568538, 3.1925558756394, 0.0673320896102137, 0.00439219348020005,
    0.135454184568538
  ), lowCI_test = c(
    86.7575949081585, 3.01971922654131,
    0.625272652671813, 3.20727591549959, 86.7575949081585, 3.01971922654131,
    0.625272652671813, 3.20727591549959, 86.7575949081585, 3.01971922654131,
    0.625272652671813, 3.20727591549959, 86.7575949081585, 3.01971922654131,
    0.625272652671813, 3.20727591549959, 79.5880486308587, 2.50336059906323,
    0.751112403965226, 0.466222367603537, 79.5880486308587, 2.50336059906323,
    0.751112403965226, 0.466222367603537, 79.5880486308587, 2.50336059906323,
    0.751112403965226, 0.466222367603537, 79.5880486308587, 2.50336059906323,
    0.751112403965226, 0.466222367603537
  ),
  hiCI_test = c(
    102.942405091841,
    3.20966779224354, 0.651588551295861, 10.6263935290328, 102.942405091841,
    3.20966779224354, 0.651588551295861, 10.6263935290328, 102.942405091841,
    3.20966779224354, 0.651588551295861, 10.6263935290328, 102.942405091841,
    3.20966779224354, 0.651588551295861, 10.6263935290328, 92.4563958135858,
    2.77475841930317, 0.768350582132071, 1.01220231040784, 92.4563958135858,
    2.77475841930317, 0.768350582132071, 1.01220231040784, 92.4563958135858,
    2.77475841930317, 0.768350582132071, 1.01220231040784, 92.4563958135858,
    2.77475841930317, 0.768350582132071, 1.01220231040784
  )
)

run_escalc <- function(x){
  escalc(n1i = n_control, 
         n2i = n_test, 
         m1i = mean_control, 
         m2i = mean_test,
         sd1i = sd_control, 
         sd2i = sd_test, 
         data = subset(dat,variable_control == x), 
         measure = "SMD",
         append = TRUE)
}

the_vars <- unique(dat[,"variable_control"])

# this returns plot.default objects that print to screen only

Map(run_escalc,the_vars) -> the_list

myfunction <- function(x) {
  forest.rma(rma(yi, vi, data = x), slab = x[[1]], header = x[[2]][1])
}

Map(myfunction,the_list) -> the_plots

the_plots
#> $Mathew
#> $Mathew$xlim
#> [1] -36.28  65.39
#> 
#> $Mathew$alim
#> [1] -10  30
#> 
#> $Mathew$at
#> [1] -10   0  10  20  30
#> 
#> $Mathew$ylim
#> [1] -1.5 11.0
#> 
#> $Mathew$rows
#> [1] 1 2 3 4 5 6 7 8
#> 
#> $Mathew$cex
#> [1] 1
#> 
#> $Mathew$cex.lab
#> [1] 1
#> 
#> $Mathew$cex.axis
#> [1] 1
#> 
#> 
#> $Simp
#> $Simp$xlim
#> [1] -47.99  63.44
#> 
#> $Simp$alim
#> [1] -10  30
#> 
#> $Simp$at
#> [1] -10   0  10  20  30
#> 
#> $Simp$ylim
#> [1] -1.5 11.0
#> 
#> $Simp$rows
#> [1] 1 2 3 4 5 6 7 8
#> 
#> $Simp$cex
#> [1] 1
#> 
#> $Simp$cex.lab
#> [1] 1
#> 
#> $Simp$cex.axis
#> [1] 1
#> 
#> 
#> $hutten
#> $hutten$xlim
#> [1] -54.10  30.23
#> 
#> $hutten$alim
#> [1] -25   5
#> 
#> $hutten$at
#> [1] -25 -20 -15 -10  -5   0   5
#> 
#> $hutten$ylim
#> [1] -1.5 11.0
#> 
#> $hutten$rows
#> [1] 1 2 3 4 5 6 7 8
#> 
#> $hutten$cex
#> [1] 1
#> 
#> $hutten$cex.lab
#> [1] 1
#> 
#> $hutten$cex.axis
#> [1] 1
#> 
#> 
#> $AB
#> $AB$xlim
#> [1] -21.86  17.49
#> 
#> $AB$alim
#> [1] -10   5
#> 
#> $AB$at
#> [1] -10  -5   0   5
#> 
#> $AB$ylim
#> [1] -1.5 11.0
#> 
#> $AB$rows
#> [1] 1 2 3 4 5 6 7 8
#> 
#> $AB$cex
#> [1] 1
#> 
#> $AB$cex.lab
#> [1] 1
#> 
#> $AB$cex.axis
#> [1] 1

# there is no way to turn these into back into plot objects;
# see lines 654-699 of source code for forest.rma
# https://github.com/wviechtb/metafor/blob/master/R/forest.rma.r
# that function would have to be modified to save the 
# plot objects

# These will work individually, however, if myfunction is
# applied not to the_list, but to Simp, Mathew, etc. extracted
# as follows

make_frame <- function(x) assign(x,data.frame(Map(run_escalc,x)[[x]]), envir = .GlobalEnv)

invisible(Map(make_frame,the_vars))

myfunction(Simp)

# you can use lapply to display them all

lapply(the_list,myfunction)

#> $Mathew
#> $Mathew$xlim
#> [1] -36.28  65.39
#> 
#> $Mathew$alim
#> [1] -10  30
#> 
#> $Mathew$at
#> [1] -10   0  10  20  30
#> 
#> $Mathew$ylim
#> [1] -1.5 11.0
#> 
#> $Mathew$rows
#> [1] 1 2 3 4 5 6 7 8
#> 
#> $Mathew$cex
#> [1] 1
#> 
#> $Mathew$cex.lab
#> [1] 1
#> 
#> $Mathew$cex.axis
#> [1] 1
#> 
#> 
#> $Simp
#> $Simp$xlim
#> [1] -47.99  63.44
#> 
#> $Simp$alim
#> [1] -10  30
#> 
#> $Simp$at
#> [1] -10   0  10  20  30
#> 
#> $Simp$ylim
#> [1] -1.5 11.0
#> 
#> $Simp$rows
#> [1] 1 2 3 4 5 6 7 8
#> 
#> $Simp$cex
#> [1] 1
#> 
#> $Simp$cex.lab
#> [1] 1
#> 
#> $Simp$cex.axis
#> [1] 1
#> 
#> 
#> $hutten
#> $hutten$xlim
#> [1] -54.10  30.23
#> 
#> $hutten$alim
#> [1] -25   5
#> 
#> $hutten$at
#> [1] -25 -20 -15 -10  -5   0   5
#> 
#> $hutten$ylim
#> [1] -1.5 11.0
#> 
#> $hutten$rows
#> [1] 1 2 3 4 5 6 7 8
#> 
#> $hutten$cex
#> [1] 1
#> 
#> $hutten$cex.lab
#> [1] 1
#> 
#> $hutten$cex.axis
#> [1] 1
#> 
#> 
#> $AB
#> $AB$xlim
#> [1] -21.86  17.49
#> 
#> $AB$alim
#> [1] -10   5
#> 
#> $AB$at
#> [1] -10  -5   0   5
#> 
#> $AB$ylim
#> [1] -1.5 11.0
#> 
#> $AB$rows
#> [1] 1 2 3 4 5 6 7 8
#> 
#> $AB$cex
#> [1] 1
#> 
#> $AB$cex.lab
#> [1] 1
#> 
#> $AB$cex.axis
#> [1] 1
1 Like

Thanks a lot @technocrat . It works. But, is not there anyway so that I can save them individually. When I used the following, it just saved the last plot only:

tiff(filename = "image.tiff", width = 30, height = 30, units = "cm", res = 1200)
Map(myfunction,the_list) -> the_plots
dev.off()

Perhaps not so hard as I thought last night.

Modify myfunction to open a device, run the logic to create the plots, save the file and then close the device. Test it on a single list element. If it works, then modify my_function to assemble the object name with the tiff extension.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.