如何在R中通过迭代函数为包含公共字符的数据帧创建列表



这是对每个结果变量进行三次重复比较的数据框架(因此子倍数级别为12)

A tibble: 36 x 10
s     .y.   group1          group2             n1    n2     p p.signif p.adj p.adj.signif
* <chr> <chr> <chr>           <chr>           <int> <int> <dbl> <chr>    <dbl> <chr>       
1 E     value new_value_for_8 new_value_for_6    25    25 0.877 ns       1     ns          
2 E     value new_value_for_8 new_value_for_4    25    25 0.929 ns       1     ns          
3 E     value new_value_for_6 new_value_for_4    25    25 0.948 ns       1     ns          
4 F     value new_value_for_8 new_value_for_6    25    25 0.735 ns       1     ns          
5 F     value new_value_for_8 new_value_for_4    25    25 0.738 ns       1     ns          
6 F     value new_value_for_6 new_value_for_4    25    25 0.501 ns       1     ns          
7 G     value new_value_for_8 new_value_for_6    25    25 0.808 ns       0.808 ns          
8 G     value new_value_for_8 new_value_for_4    25    25 0.101 ns       0.303 ns          
9 G     value new_value_for_6 new_value_for_4    25    25 0.161 ns       0.321 ns          
10 H     value new_value_for_8 new_value_for_6    25    25 0.964 ns       0.964 ns 

我刚刚为三个重复度量中的每一个创建了一个包含12个元素的列表,其中包含相关统计信息,如下所示:

my_comparisons <- list(E = comparisons[1:3,], 
F = comparisons[4:6,], 
G = comparisons[7:9,], 
H = comparisons[10:12,], 
I = comparisons[13:15,], 
J = comparisons[16:18,], 
K =comparisons[19:21,], 
L = comparisons[22:24,], 
M = comparisons[25:27,], 
N = comparisons[28:30,], 
O  = comparisons[31:33,], 
P = comparisons[34:36,])

得到以下结果

[[E]]
# A tibble: 3 x 10
s     .y.   group1          group2             n1    n2     p p.signif p.adj p.adj.signif
<chr> <chr> <chr>           <chr>           <int> <int> <dbl> <chr>    <dbl> <chr>       
1 E     value new_value_for_8 new_value_for_6    25    25 0.877 ns           1 ns          
2 E     value new_value_for_8 new_value_for_4    25    25 0.929 ns           1 ns          
3 E     value new_value_for_6 new_value_for_4    25    25 0.948 ns           1 ns  
[[F]] .... and so on

既然你可以在信号列中看到,根据它,我把列表分成了一些常见的字体(例如P3, FCz, lpearly等)。我想使用一个迭代函数,如lapply(),一些循环或map()函数来自动创建这个列表。

Thanks in advance

这里原始数据集

> dput(head(df, 10))
structure(list(A = 1:10, C = c("Maybe", "Maybe", "Maybe", "Maybe", 
"Maybe", "Maybe", "Maybe", "Maybe", "Maybe", "Maybe"), D = structure(c(1L, 
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L), .Label = c("new_value_for_8", 
"new_value_for_6", "new_value_for_4"), class = "factor"), E = c(988.368784828308, 
988.856158671407, 996.004085290553, 999.685844324618, 1000.23888564896, 
1005.03749946898, 999.786378084971, 997.039675082569, 998.028313183065, 
997.168905747014), F = c(994.834756009939, 994.468875098246, 
1000.62150212342, 1002.23100741241, 1003.96990710863, 1007.75899775608, 
998.699806256246, 996.401009591011, 998.076594704249, 1002.19344184533
), G = c(1011.88022669726, 1012.10534266625, 1012.9554415821, 
1015.09810043606, 1015.40462298842, 1016.67103699915, 1003.13771453335, 
999.9107434841, 1002.15365554737, 1013.67789244066), H = c(988.221495702721, 
990.850727928741, 992.418094914622, 995.984841639886, 993.398346143465, 
997.971380355398, 1004.4672957051, 1002.54036572775, 1002.2292388993, 
999.116379988893), I = c(994.035709684742, 994.890815628412, 
997.18267770374, 998.564426335124, 996.851278420874, 1000.16039368502, 
1003.52155765272, 1002.1043798945, 1002.7069399281, 1005.49897156208
), J = c(1008.23981597718, 1009.51261484649, 1009.42367409926, 
1005.06332653216, 1005.02619159395, 1009.07903916629, 1007.56089165218, 
1005.49719893791, 1004.91476855238, 1013.03209535721), K = c(994.327042030287, 
995.608170991922, 997.033470393412, 1000.15918365269, 998.216388150646, 
1001.97377908784, 1003.17401220482, 1001.60211665164, 1002.27932356239, 
1002.41479226363), L = c(999.225538268699, 999.349990537239, 
1001.14010250645, 1001.51403741206, 1000.25571835554, 1003.76051565494, 
1002.74763442988, 1001.09116707486, 1003.29833843754, 1006.55857216695
), M = c(1009.99385579756, 1011.12126521731, 1010.6989716872, 
1003.7899021821, 1004.59413830322, 1008.52123662618, 1006.34418311104, 
1004.1077131243, 1004.94124365003, 1011.89121961563), N = c(999.801263745036, 
996.838989582336, 1000.89599227983, 1003.11042068113, 1002.27800090558, 
1003.83846437952, 1000.70169995102, 1001.75290674649, 998.660833714301, 
1006.69246804854), O = c(1002.96437294923, 997.870867692911, 
1002.94619035116, 1003.44844607015, 1003.02403433836, 1004.70457675466, 
999.880559826981, 1000.66826545719, 999.59436981446, 1007.32640154038
), P = c(1006.28027312932, 1005.24535230967, 1007.68162285336, 
1001.08242973466, 1002.99896314, 1005.36085942954, 1001.22060069797, 
1000.43007709819, 1000.47666761108, 1008.73650967215)), row.names = c(NA, 
10L), class = "data.frame")

我猜,您正在寻找如下内容:

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

最新更新