Home > OS >  How to create in R a list for a dataframe containg common character through an iterative function
How to create in R a list for a dataframe containg common character through an iterative function

Time:10-16

this is the dataframe with trice repeated comparisons for every outcome variable (so the submutiple level is 12)

    > as.data.frame(comparisons)
           signals   .y.  group1  group2 n1 n2   statistic df        p    p.adj p.adj.signif
    1        P3FCz value NEG-CTR NEG-NOC 25 25 -0.32183284 24 0.750000 1.000000           ns
    2        P3FCz value NEG-CTR NEU-NOC 25 25 -0.17788461 24 0.860000 1.000000           ns
    3        P3FCz value NEG-NOC NEU-NOC 25 25  0.11249149 24 0.911000 1.000000           ns
    4         P3Cz value NEG-CTR NEG-NOC 25 25 -0.62380748 24 0.539000 1.000000           ns
    5         P3Cz value NEG-CTR NEU-NOC 25 25  0.59236111 24 0.559000 1.000000           ns
    6         P3Cz value NEG-NOC NEU-NOC 25 25  0.92477314 24 0.364000 1.000000           ns
    7         P3Pz value NEG-CTR NEG-NOC 25 25  0.43979736 24 0.664000 1.000000           ns
    8         P3Pz value NEG-CTR NEU-NOC 25 25  3.10746654 24 0.005000 0.014000            *
    9         P3Pz value NEG-NOC NEU-NOC 25 25  2.42892310 24 0.023000 0.069000           ns
    10 LPPearlyFCz value NEG-CTR NEG-NOC 25 25 -0.09188784 24 0.928000 1.000000           ns
    11 LPPearlyFCz value NEG-CTR NEU-NOC 25 25  2.31385915 24 0.030000 0.089000           ns
    12 LPPearlyFCz value NEG-NOC NEU-NOC 25 25  2.30243506 24 0.030000 0.091000           ns
    13  LPPearlyCz value NEG-CTR NEG-NOC 25 25 -0.36897352 24 0.715000 1.000000           ns
    14  LPPearlyCz value NEG-CTR NEU-NOC 25 25  3.28159273 24 0.003000 0.009000           **
    15  LPPearlyCz value NEG-NOC NEU-NOC 25 25  3.09240265 24 0.005000 0.015000            *
    16  LPPearlyPz value NEG-CTR NEG-NOC 25 25  0.06703844 24 0.947000 1.000000           ns
    17  LPPearlyPz value NEG-CTR NEU-NOC 25 25  4.25913230 24 0.000273 0.000819          ***
    18  LPPearlyPz value NEG-NOC NEU-NOC 25 25  4.43158703 24 0.000176 0.000528          ***
    19     LPP1FCz value NEG-CTR NEG-NOC 25 25 -0.39439158 24 0.697000 1.000000           ns
    20     LPP1FCz value NEG-CTR NEU-NOC 25 25  2.53611856 24 0.018000 0.054000           ns
    21     LPP1FCz value NEG-NOC NEU-NOC 25 25  2.36271993 24 0.027000 0.080000           ns
    22      LPP1Cz value NEG-CTR NEG-NOC 25 25 -1.06592362 24 0.297000 0.891000           ns
    23      LPP1Cz value NEG-CTR NEU-NOC 25 25  2.77405996 24 0.011000 0.032000            *
    24      LPP1Cz value NEG-NOC NEU-NOC 25 25  3.06325458 24 0.005000 0.016000            *
    25      LPP1Pz value NEG-CTR NEG-NOC 25 25 -0.54210261 24 0.593000 1.000000           ns
    26      LPP1Pz value NEG-CTR NEU-NOC 25 25  3.72755117 24 0.001000 0.003000           **
    27      LPP1Pz value NEG-NOC NEU-NOC 25 25  4.31056245 24 0.000240 0.000720          ***
    28     LPP2FCz value NEG-CTR NEG-NOC 25 25 -0.58228303 24 0.566000 1.000000           ns
    29     LPP2FCz value NEG-CTR NEU-NOC 25 25  0.10238271 24 0.919000 1.000000           ns
    30     LPP2FCz value NEG-NOC NEU-NOC 25 25  0.58654953 24 0.563000 1.000000           ns
    31      LPP2Cz value NEG-CTR NEG-NOC 25 25 -1.32163941 24 0.199000 0.597000           ns
    32      LPP2Cz value NEG-CTR NEU-NOC 25 25  0.02393817 24 0.981000 1.000000           ns
    33      LPP2Cz value NEG-NOC NEU-NOC 25 25  1.13763114 24 0.267000 0.801000           ns
    34      LPP2Pz value NEG-CTR NEG-NOC 25 25 -1.63511147 24 0.115000 0.345000           ns
    35      LPP2Pz value NEG-CTR NEU-NOC 25 25  0.87003960 24 0.393000 1.000000           ns
    36      LPP2Pz value NEG-NOC NEU-NOC 25 25  2.10635863 24 0.046000 0.137000           ns
    > 

I've just created a 12-element list for each of the three repetaed measures containing relative statistics as follows:

my_comparisons <- list(P3FCz = comparisons[1:3,], 
                       P3Cz = comparisons[4:6,], 
                       P3Pz = comparisons[7:9,], 
                       LPPearlyFcz = comparisons[10:12,], 
                       LPPearlyCz = comparisons[13:15,], 
                       LPPearlyPz = comparisons[16:18,], 
                       LPP1FCz =comparisons[19:21,], 
                       LPP1Cz = comparisons[22:24,], 
                       LPP1Pz = comparisons[25:27,], 
                       LPP2FCz = comparisons[28:30,], 
                       LPP2Cz  = comparisons[31:33,], 
                       LPP2Pz = comparisons[34:36,])

obtaining the following results

[[P3FCz]]
    y.  group1  group2 n1 n2   statistic df        p.......
 value NEG-CTR NEG-NOC 25 25 -0.32183284 24 0.750000 ...
 value NEG-CTR NEU-NOC 25 25 -0.17788461 24 0.860000 ....
 value NEG-NOC NEU-NOC 25 25  0.11249149 24 0.911000 ....

[[P3Cz]] .... and so on

Since how you could see in the signals columns, according to which I split the list there some common font (for instance P3, FCz, LPPearly and so on). I would like to use an iterate function such lapply(), some loops or map() functions in order to automatize this list creation.

Thanks in advance

Here the original dataset

> dput(head(df_join))
structure(list(ID = c("01", "01", "01", "04", "04", "04"), GR = c("RP", 
"RP", "RP", "RP", "RP", "RP"), SES = c("V", "V", "V", "V", "V", 
"V"), COND = structure(c(1L, 2L, 3L, 1L, 2L, 3L), .Label = c("NEG-CTR", 
"NEG-NOC", "NEU-NOC"), class = "factor"), P3FCz = c(-11.6312151716924, 
-11.1438413285935, -3.99591470944713, -0.314155675382471, 0.238885648959708, 
5.03749946898385), P3Cz = c(-5.16524399006139, -5.53112490175437, 
0.621502123415388, 2.23100741241039, 3.96990710862955, 7.75899775608441
), P3Pz = c(11.8802266972569, 12.1053426662461, 12.955441582096, 
15.0981004360619, 15.4046229884164, 16.671036999147), LPPearlyFCz = c(-11.7785042972793, 
-9.14927207125904, -7.58190508537766, -4.01515836011381, -6.60165385653499, 
-2.02861964460179), LPPearlyCz = c(-5.96429031525769, -5.10918437158799, 
-2.81732229625975, -1.43557366487622, -3.14872157912645, 0.160393685024631
), LPPearlyPz = c(8.23981597718437, 9.51261484648731, 9.42367409925817, 
5.06332653216481, 5.02619159395405, 9.07903916629231), LPP1FCz = c(-5.67295796971287, 
-4.3918290080777, -2.96652960658775, 0.159183652691071, -1.78361184935376, 
1.97377908783621), LPP1Cz = c(-0.774461731301161, -0.650009462761383, 
1.14010250644923, 1.51403741206392, 0.25571835554024, 3.76051565494304
), LPP1Pz = c(9.99385579756163, 11.1212652173052, 10.6989716871958, 
3.7899021820967, 4.59413830322224, 8.52123662617732), LPP2FCz = c(-0.198736254963744, 
-3.16101041766438, 0.895992279831378, 3.11042068112836, 2.27800090558473, 
3.83846437952292), LPP2Cz = c(2.96437294922766, -2.12913230708907, 
2.94619035115619, 3.44844607014521, 3.02403433835637, 4.7045767546583
), LPP2Pz = c(6.28027312932027, 5.24535230966772, 7.68162285335806, 
1.08242973465635, 2.99896314000211, 5.36085942954182)), row.names = c(NA, 
6L), class = "data.frame")
> 

CodePudding user response:

I guess, you are looking for something like the following:

library(dplyr)

df <- structure(list(signals = c("P3FCz", "P3FCz", "P3FCz", "P3Cz", 
"P3Cz", "P3Cz", "P3Pz", "P3Pz", "P3Pz", "LPPearlyFCz", "LPPearlyFCz", 
"LPPearlyFCz", "LPPearlyCz", "LPPearlyCz", "LPPearlyCz", "LPPearlyPz", 
"LPPearlyPz", "LPPearlyPz", "LPP1FCz", "LPP1FCz", "LPP1FCz", 
"LPP1Cz", "LPP1Cz", "LPP1Cz", "LPP1Pz", "LPP1Pz", "LPP1Pz", "LPP2FCz", 
"LPP2FCz", "LPP2FCz", "LPP2Cz", "LPP2Cz", "LPP2Cz", "LPP2Pz", 
"LPP2Pz", "LPP2Pz"), .y. = c("value", "value", "value", "value", 
"value", "value", "value", "value", "value", "value", "value", 
"value", "value", "value", "value", "value", "value", "value", 
"value", "value", "value", "value", "value", "value", "value", 
"value", "value", "value", "value", "value", "value", "value", 
"value", "value", "value", "value"), group1 = c("NEG-CTR", "NEG-CTR", 
"NEG-NOC", "NEG-CTR", "NEG-CTR", "NEG-NOC", "NEG-CTR", "NEG-CTR", 
"NEG-NOC", "NEG-CTR", "NEG-CTR", "NEG-NOC", "NEG-CTR", "NEG-CTR", 
"NEG-NOC", "NEG-CTR", "NEG-CTR", "NEG-NOC", "NEG-CTR", "NEG-CTR", 
"NEG-NOC", "NEG-CTR", "NEG-CTR", "NEG-NOC", "NEG-CTR", "NEG-CTR", 
"NEG-NOC", "NEG-CTR", "NEG-CTR", "NEG-NOC", "NEG-CTR", "NEG-CTR", 
"NEG-NOC", "NEG-CTR", "NEG-CTR", "NEG-NOC"), group2 = c("NEG-NOC", 
"NEU-NOC", "NEU-NOC", "NEG-NOC", "NEU-NOC", "NEU-NOC", "NEG-NOC", 
"NEU-NOC", "NEU-NOC", "NEG-NOC", "NEU-NOC", "NEU-NOC", "NEG-NOC", 
"NEU-NOC", "NEU-NOC", "NEG-NOC", "NEU-NOC", "NEU-NOC", "NEG-NOC", 
"NEU-NOC", "NEU-NOC", "NEG-NOC", "NEU-NOC", "NEU-NOC", "NEG-NOC", 
"NEU-NOC", "NEU-NOC", "NEG-NOC", "NEU-NOC", "NEU-NOC", "NEG-NOC", 
"NEU-NOC", "NEU-NOC", "NEG-NOC", "NEU-NOC", "NEU-NOC"), n1 = c(25L, 
25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 
25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 
25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L), n2 = c(25L, 25L, 
25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 
25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 
25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L), statistic = c(-0.32183284, 
-0.17788461, 0.11249149, -0.62380748, 0.59236111, 0.92477314, 
0.43979736, 3.10746654, 2.4289231, -0.09188784, 2.31385915, 2.30243506, 
-0.36897352, 3.28159273, 3.09240265, 0.06703844, 4.2591323, 4.43158703, 
-0.39439158, 2.53611856, 2.36271993, -1.06592362, 2.77405996, 
3.06325458, -0.54210261, 3.72755117, 4.31056245, -0.58228303, 
0.10238271, 0.58654953, -1.32163941, 0.02393817, 1.13763114, 
-1.63511147, 0.8700396, 2.10635863), df = c(24L, 24L, 24L, 24L, 
24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 
24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 
24L, 24L, 24L, 24L, 24L, 24L), p = c(0.75, 0.86, 0.911, 0.539, 
0.559, 0.364, 0.664, 0.005, 0.023, 0.928, 0.03, 0.03, 0.715, 
0.003, 0.005, 0.947, 0.000273, 0.000176, 0.697, 0.018, 0.027, 
0.297, 0.011, 0.005, 0.593, 0.001, 0.00024, 0.566, 0.919, 0.563, 
0.199, 0.981, 0.267, 0.115, 0.393, 0.046), p.adj = c(1, 1, 1, 
1, 1, 1, 1, 0.014, 0.069, 1, 0.089, 0.091, 1, 0.009, 0.015, 1, 
0.000819, 0.000528, 1, 0.054, 0.08, 0.891, 0.032, 0.016, 1, 0.003, 
0.00072, 1, 1, 1, 0.597, 1, 0.801, 0.345, 1, 0.137), p.adj.signif = c("ns", 
"ns", "ns", "ns", "ns", "ns", "ns", "*", "ns", "ns", "ns", "ns", 
"ns", "**", "*", "ns", "***", "***", "ns", "ns", "ns", "ns", 
"*", "*", "ns", "**", "***", "ns", "ns", "ns", "ns", "ns", "ns", 
"ns", "ns", "\n")), row.names = c(NA, -36L), class = "data.frame")

df %>% 
  group_split(signals) %>% 
  as.list() %>% 
  setNames(sort(unique(df$signals)))
  • Related