我有这个列表,由ID,电极名称(以FP1,FP2等开头)和这些电极的值(与电极编号相同的长度)组成。 来自前三个主题(ID01,ID02等)的数据会粘贴到此处,直到300为止,因此前三个元素构成了一个主题的数据。 (300个中的第二个)是第二个主题,第三个三个是第三个主题,.... 如何将它们放入数据框中,因此第一列将是ID,其余列将是电极名称,值将填充相应的电极列? 并非所有受试者都具有所有电极,但是目标是所有电极名称的列表(以列为单位),而对于在所有电极上都没有值的受试者,NA为NA。最后一个主题包含所有电极的完整列表(n = 61)。
list(structure(c(3.45795372430056, 3.53200270642221, 4.36553103359199,
3.05971192770099, 3.29632877899404, 3.31308236318143, 4.24725416194864,
3.66643658172688, 2.73649182050675, 2.86257080026395, 3.45461249221543,
3.53379688684901, 2.65509461019589, 5.32702317286538, 5.1440372077419,
8.00426340158606, 3.92566337555864, 2.9636836702809, 7.79363529740775,
3.42717498319627, 12.5073419593008, 10.3913035195863, 5.44169225429074,
7.85383530152502, 11.3143739506589, 16.5073444629463, 17.5407670530062,
12.5893645313808, 17.8699704499684, 8.48272564646519, 3.55173967332533,
3.26248745488676, 3.47497470296353, 3.62100148748729, 3.18396151227945,
3.14692007975935, 3.22899853274755, 3.54879001434911, 5.06844813509229,
2.57729130540013, 3.03469400084275, 4.28207212770906, 4.21050720100374,
2.18076346859284, 2.40017232044327, 4.90875033326619, 7.16821281572614,
2.54824918527749, 7.15790382478216, 5.20483882884626, 10.7570196069246,
7.54524246493832, 4.95407437226951, 11.2942158383124, 22.0250910762801,
14.0146020891073, 14.2275533653629, 13.2304661228916, 25.3693988199125
), .Dim = c(1L, 59L)), structure("010002", .Dim = c(1L, 1L)),
list(structure(list(structure("Fp1", .Dim = c(1L, 1L))), .Names = ""),
structure(list(structure("Fp2", .Dim = c(1L, 1L))), .Names = ""),
structure(list(structure("F7", .Dim = c(1L, 1L))), .Names = ""),
structure(list(structure("F3", .Dim = c(1L, 1L))), .Names = ""),
structure(list(structure("Fz", .Dim = c(1L, 1L))), .Names = ""),
structure(list(structure("F4", .Dim = c(1L, 1L))), .Names = ""),
structure(list(structure("F8", .Dim = c(1L, 1L))), .Names = ""),
structure(list(structure("FC5", .Dim = c(1L, 1L))), .Names = ""),
structure(list(structure("FC1", .Dim = c(1L, 1L))), .Names = ""),
structure(list(structure("FC2", .Dim = c(1L, 1L))), .Names = ""),
structure(list(structure("FC6", .Dim = c(1L, 1L))), .Names = ""),
structure(list(structure("C3", .Dim = c(1L, 1L))), .Names = ""),
structure(list(structure("Cz", .Dim = c(1L, 1L))), .Names = ""),
structure(list(structure("C4", .Dim = c(1L, 1L))), .Names = ""),
structure(list(structure("T8", .Dim = c(1L, 1L))), .Names = ""),
structure(list(structure("CP5", .Dim = c(1L, 1L))), .Names = ""),
structure(list(structure("CP1", .Dim = c(1L, 1L))), .Names = ""),
structure(list(structure("CP2", .Dim = c(1L, 1L))), .Names = ""),
structure(list(structure("CP6", .Dim = c(1L, 1L))), .Names = ""),
structure(list(structure("AFz", .Dim = c(1L, 1L))), .Names = ""),
structure(list(structure("P7", .Dim = c(1L, 1L))), .Names = ""),
structure(list(structure("P3", .Dim = c(1L, 1L))), .Names = ""),
structure(list(structure("Pz", .Dim = c(1L, 1L))), .Names = ""),
structure(list(structure("P4", .Dim = c(1L, 1L))), .Names = ""),
structure(list(structure("P8", .Dim = c(1L, 1L))), .Names = ""),
structure(list(structure("PO9", .Dim = c(1L, 1L))), .Names = ""),
structure(list(structure("O1", .Dim = c(1L, 1L))), .Names = ""),
structure(list(structure("Oz", .Dim = c(1L, 1L))), .Names = ""),
structure(list(structure("O2", .Dim = c(1L, 1L))), .Names = ""),
structure(list(structure("PO10", .Dim = c(1L, 1L))), .Names = ""),
structure(list(structure("AF7", .Dim = c(1L, 1L))), .Names = ""),
structure(list(structure("AF3", .Dim = c(1L, 1L))), .Names = ""),
structure(list(structure("AF4", .Dim = c(1L, 1L))), .Names = ""),
structure(list(structure("AF8", .Dim = c(1L, 1L))), .Names = ""),
structure(list(structure("F5", .Dim = c(1L, 1L))), .Names = ""),
structure(list(structure("F1", .Dim = c(1L, 1L))), .Names = ""),
structure(list(structure("F2", .Dim = c(1L, 1L))), .Names = ""),
structure(list(structure("F6", .Dim = c(1L, 1L))), .Names = ""),
structure(list(structure("FT7", .Dim = c(1L, 1L))), .Names = ""),
structure(list(structure("FC3", .Dim = c(1L, 1L))), .Names = ""),
structure(list(structure("FC4", .Dim = c(1L, 1L))), .Names = ""),
structure(list(structure("FT8", .Dim = c(1L, 1L))), .Names = ""),
structure(list(structure("C5", .Dim = c(1L, 1L))), .Names = ""),
structure(list(structure("C1", .Dim = c(1L, 1L))), .Names = ""),
structure(list(structure("C2", .Dim = c(1L, 1L))), .Names = ""),
structure(list(structure("C6", .Dim = c(1L, 1L))), .Names = ""),
structure(list(structure("CP3", .Dim = c(1L, 1L))), .Names = ""),
structure(list(structure("CPz", .Dim = c(1L, 1L))), .Names = ""),
structure(list(structure("CP4", .Dim = c(1L, 1L))), .Names = ""),
structure(list(structure("TP8", .Dim = c(1L, 1L))), .Names = ""),
structure(list(structure("P5", .Dim = c(1L, 1L))), .Names = ""),
structure(list(structure("P1", .Dim = c(1L, 1L))), .Names = ""),
structure(list(structure("P2", .Dim = c(1L, 1L))), .Names = ""),
structure(list(structure("P6", .Dim = c(1L, 1L))), .Names = ""),
structure(list(structure("PO7", .Dim = c(1L, 1L))), .Names = ""),
structure(list(structure("PO3", .Dim = c(1L, 1L))), .Names = ""),
structure(list(structure("POz", .Dim = c(1L, 1L))), .Names = ""),
structure(list(structure("PO4", .Dim = c(1L, 1L))), .Names = ""),
structure(list(structure("PO8", .Dim = c(1L, 1L))), .Names = "")),
structure(c(26.8384489065394, 26.178786435345, 24.0696274719001,
22.0581009753417, 21.7966235494399, 20.3346419268736, 20.0605899957186,
17.7991738540519, 18.7540314511323, 17.9510904574407, 16.8521197344211,
19.6842338986443, 18.9743577851434, 21.4202581870846, 19.3416505488144,
17.7356454173219, 19.4875220168306, 21.9753445773492, 17.5913570320386,
24.5242414281308, 43.8746438160429, 40.3635608443272, 31.4058554541589,
40.9379373855574, 49.782682122153, 39.3376883823783, 96.8794416352117,
86.1996398947607, 106.634503783336, 38.7159635810776, 26.347589142127,
24.7225308421884, 23.4233625744009, 23.5325301411389, 23.0320199288561,
21.4952861744385, 20.6886343868372, 20.1221952048054, 20.0772452406375,
17.6885410768851, 16.8476207646853, 18.4221038053744, 17.1960298481726,
17.1211073274811, 19.1537187755581, 16.5737459444849, 23.8639523921583,
19.7756724284431, 20.6199232123305, 23.894234308329, 25.8977179919594,
44.7853496906293, 33.6182326942625, 33.6419014416442, 44.6089201552485,
92.39219248212, 124.816871412174, 90.0410019162686, 114.824488016676,
104.651494384023), .Dim = c(1L, 60L)), structure("010003", .Dim = c(1L,
1L)), list(structure(list(structure("Fp1", .Dim = c(1L, 1L
))), .Names = ""), structure(list(structure("Fp2", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("F7", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("F3", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("Fz", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("F4", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("F8", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("FC5", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("FC1", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("FC2", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("FC6", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("C3", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("Cz", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("C4", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("T8", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("CP5", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("CP1", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("CP2", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("CP6", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("AFz", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("P7", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("P3", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("Pz", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("P4", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("P8", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("PO9", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("O1", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("Oz", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("O2", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("PO10", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("AF7", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("AF3", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("AF4", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("AF8", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("F5", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("F1", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("F2", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("F6", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("FT7", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("FC3", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("FC4", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("FT8", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("C5", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("C1", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("C2", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("C6", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("TP7", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("CP3", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("CPz", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("CP4", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("TP8", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("P5", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("P1", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("P2", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("P6", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("PO7", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("PO3", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("POz", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("PO4", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("PO8", .Dim = c(1L,
1L))), .Names = "")), structure(c(6.52262355115135, 6.46895430321081,
9.96846249894624, 5.91667626881474, 5.52951310466457, 5.64831450355776,
8.02098150950742, 6.38999209902158, 4.79760752845684, 5.26073757775495,
5.08580860086334, 6.24470367065521, 3.94704299223443, 4.16346118185809,
4.25535538782261, 8.38621701212565, 6.51193287638641, 3.81564335255996,
3.83327398262525, 7.70669254303875, 6.00986043034125, 12.7066628054173,
7.23785194772164, 4.48524677154324, 5.75359331597233, 22.0131273871584,
11.416930149232, 16.6470763469124, 10.3586383578097, 22.2825021291186,
19.6767724196017, 6.69851406360227, 5.86261428741011, 5.89256893341301,
7.4236395972263, 6.36962887486336, 5.48924913926016, 5.68968158677107,
5.6087195451651, 10.4806687884767, 4.53619567023033, 4.65711761775388,
7.19445136113883, 6.16464848622524, 3.78797139799689, 4.09748540901898,
5.23320880832957, 11.8566417647642, 4.82329124268468, 3.4392192645961,
5.36288682401034, 12.7443548964839, 9.50046297804874, 5.25521906440384,
5.77644207387369, 14.6796214279794, 18.1397580727737, 11.7588661192153,
8.09118982724719, 12.3338712811978, 25.9414299375728), .Dim = c(1L,
61L)), structure("010004", .Dim = c(1L, 1L)), list(structure(list(
structure("Fp1", .Dim = c(1L, 1L))), .Names = ""), structure(list(
structure("Fp2", .Dim = c(1L, 1L))), .Names = ""), structure(list(
structure("F7", .Dim = c(1L, 1L))), .Names = ""), structure(list(
structure("F3", .Dim = c(1L, 1L))), .Names = ""), structure(list(
structure("Fz", .Dim = c(1L, 1L))), .Names = ""), structure(list(
structure("F4", .Dim = c(1L, 1L))), .Names = ""), structure(list(
structure("F8", .Dim = c(1L, 1L))), .Names = ""), structure(list(
structure("FC5", .Dim = c(1L, 1L))), .Names = ""), structure(list(
structure("FC1", .Dim = c(1L, 1L))), .Names = ""), structure(list(
structure("FC2", .Dim = c(1L, 1L))), .Names = ""), structure(list(
structure("FC6", .Dim = c(1L, 1L))), .Names = ""), structure(list(
structure("T7", .Dim = c(1L, 1L))), .Names = ""), structure(list(
structure("C3", .Dim = c(1L, 1L))), .Names = ""), structure(list(
structure("Cz", .Dim = c(1L, 1L))), .Names = ""), structure(list(
structure("C4", .Dim = c(1L, 1L))), .Names = ""), structure(list(
structure("T8", .Dim = c(1L, 1L))), .Names = ""), structure(list(
structure("CP5", .Dim = c(1L, 1L))), .Names = ""), structure(list(
structure("CP1", .Dim = c(1L, 1L))), .Names = ""), structure(list(
structure("CP2", .Dim = c(1L, 1L))), .Names = ""), structure(list(
structure("CP6", .Dim = c(1L, 1L))), .Names = ""), structure(list(
structure("AFz", .Dim = c(1L, 1L))), .Names = ""), structure(list(
structure("P7", .Dim = c(1L, 1L))), .Names = ""), structure(list(
structure("P3", .Dim = c(1L, 1L))), .Names = ""), structure(list(
structure("Pz", .Dim = c(1L, 1L))), .Names = ""), structure(list(
structure("P4", .Dim = c(1L, 1L))), .Names = ""), structure(list(
structure("P8", .Dim = c(1L, 1L))), .Names = ""), structure(list(
structure("PO9", .Dim = c(1L, 1L))), .Names = ""), structure(list(
structure("O1", .Dim = c(1L, 1L))), .Names = ""), structure(list(
structure("Oz", .Dim = c(1L, 1L))), .Names = ""), structure(list(
structure("O2", .Dim = c(1L, 1L))), .Names = ""), structure(list(
structure("PO10", .Dim = c(1L, 1L))), .Names = ""), structure(list(
structure("AF7", .Dim = c(1L, 1L))), .Names = ""), structure(list(
structure("AF3", .Dim = c(1L, 1L))), .Names = ""), structure(list(
structure("AF4", .Dim = c(1L, 1L))), .Names = ""), structure(list(
structure("AF8", .Dim = c(1L, 1L))), .Names = ""), structure(list(
structure("F5", .Dim = c(1L, 1L))), .Names = ""), structure(list(
structure("F1", .Dim = c(1L, 1L))), .Names = ""), structure(list(
structure("F2", .Dim = c(1L, 1L))), .Names = ""), structure(list(
structure("F6", .Dim = c(1L, 1L))), .Names = ""), structure(list(
structure("FT7", .Dim = c(1L, 1L))), .Names = ""), structure(list(
structure("FC3", .Dim = c(1L, 1L))), .Names = ""), structure(list(
structure("FC4", .Dim = c(1L, 1L))), .Names = ""), structure(list(
structure("FT8", .Dim = c(1L, 1L))), .Names = ""), structure(list(
structure("C5", .Dim = c(1L, 1L))), .Names = ""), structure(list(
structure("C1", .Dim = c(1L, 1L))), .Names = ""), structure(list(
structure("C2", .Dim = c(1L, 1L))), .Names = ""), structure(list(
structure("C6", .Dim = c(1L, 1L))), .Names = ""), structure(list(
structure("TP7", .Dim = c(1L, 1L))), .Names = ""), structure(list(
structure("CP3", .Dim = c(1L, 1L))), .Names = ""), structure(list(
structure("CPz", .Dim = c(1L, 1L))), .Names = ""), structure(list(
structure("CP4", .Dim = c(1L, 1L))), .Names = ""), structure(list(
structure("TP8", .Dim = c(1L, 1L))), .Names = ""), structure(list(
structure("P5", .Dim = c(1L, 1L))), .Names = ""), structure(list(
structure("P1", .Dim = c(1L, 1L))), .Names = ""), structure(list(
structure("P2", .Dim = c(1L, 1L))), .Names = ""), structure(list(
structure("P6", .Dim = c(1L, 1L))), .Names = ""), structure(list(
structure("PO7", .Dim = c(1L, 1L))), .Names = ""), structure(list(
structure("PO3", .Dim = c(1L, 1L))), .Names = ""), structure(list(
structure("POz", .Dim = c(1L, 1L))), .Names = ""), structure(list(
structure("PO4", .Dim = c(1L, 1L))), .Names = ""), structure(list(
structure("PO8", .Dim = c(1L, 1L))), .Names = "")))
最后,我想要一个像这样的数据集:
structure(list(ID = 11:13, Fp1 = c(1.263922213, 62.32026167,
NA), Fp2 = c(1.091686849, 62.4886991, NA), F7 = c(0.984334333,
52.31954112, NA), F3 = c(1.096631569, 51.56342594, NA), Fz = c(1.351514783,
55.32414052, NA), F4 = c(1.218890315, 52.00981554, NA), F8 = c(0.914644708,
46.68492663, NA), FC5 = c(0.601500911, 35.56623024, NA), FC1 = c(1.177340834,
36.6661937, NA), FC2 = c(1.417423697, 39.36278479, NA), FC6 = c(0.683356763,
32.63064211, NA), T7 = c(1.278134852, 21.10633206, NA), C3 = c(0.512067552,
13.69190204, NA), Cz = c(1.408020324, 24.46806549, NA), C4 = c(0.81822268,
13.63535186, NA), T8 = c(0.850876192, 20.05841876, NA), CP5 = c(1.507355579,
14.74676578, NA), CP1 = c(0.593112661, 15.66113117, NA), CP2 = c(0.792048927,
11.8129256, NA), CP6 = c(0.681988768, 15.46479391, NA), Afz = c(1.238921666,
60.94917394, NA), P7 = c(4.75862455, 83.42534527, NA), P3 = c(1.488444506,
52.65631279, NA), Pz = c(0.747210746, 61.51374412, NA), P4 = c(0.752375408,
46.99538186, NA), P8 = c(2.538073468, 125.5516832, NA), PO9 = c(8.413214469,
104.68268, NA), O1 = c(6.367353389, 232.7845208, NA), Oz = c(2.263346576,
170.7459949, NA), O2 = c(3.10798708, 211.1910123, NA), PO10 = c(3.17989848,
106.6299255, NA), AF7 = c(1.12513852, 59.08884192, NA), AF3 = c(1.156310804,
59.35637981, NA), AF4 = c(1.134953069, 58.97170076, NA), AF8 = c(1.150562012,
56.06065559, NA), F5 = c(0.833960928, 53.59838273, NA), F1 = c(1.353046814,
52.44991656, NA), F2 = c(1.429154713, 53.76936818, NA), F6 = c(1.008437819,
50.81219875, NA), FT7 = c(0.974090365, 36.81396512, NA), FC3 = c(0.709146544,
34.35878924, NA), FC4 = c(1.019948885, 33.34821677, NA), FT8 = c(0.825177004,
32.26045906, NA), C5 = c(0.663452106, 17.20344674, NA), C1 = c(0.796890294,
15.99710212, NA), C2 = c(1.051848935, 16.06017515, NA), C6 = c(0.569041657,
15.74648884, NA), TP7 = c(2.489287907, 17.50258745, NA), CP3 = c(0.801137141,
12.66043828, NA), CPz = c(0.924721845, 17.94083825, NA), CP4 = c(0.705080811,
9.451619285, NA), TP8 = c(1.049751438, 31.21243546, NA), P5 = c(2.881512895,
72.87366809, NA), P1 = c(0.86758872, 52.54723734, NA), P2 = c(0.683790151,
47.5009376, NA), P6 = c(1.357360304, 86.92030525, NA), PO7 = c(9.037283791,
290.4541348, NA), PO3 = c(2.86514785, 190.1773559, NA), Poz = c(1.210950625,
229.3585099, NA), PO4 = c(1.426158747, 185.6488768, NA), PO8 = c(4.372615653,
296.6505658, NA)), row.names = 11:13, class = "data.frame")
我尝试了许多(可能令人尴尬的)方法,但都以失败告终。我从来不需要处理列表,所以任何帮助将不胜感激!谢谢
答案 0 :(得分:0)
我花了一些时间试图弄清楚如何以一种理想的方式使用lapply
来塑造您的数据,但是我选择使用for
循环(也许其他人可以找到更好的方法来做到这一点!)。背后的一般逻辑是取消列出您的列表以获取我可以使用的数据框,然后使用tidyverse
重塑您的data.frame。
NewList <- vector("list", length(data)/3)
for (i in 1:(length(data)/3)) {
NewList[[i]] <- as.data.frame(t(rbind(unlist(data[[3*i - 2]]), unlist(data[[3*i - 1]]), unlist(data[[3*i]]))))
}
NewDF <- rbindlist(check)
colnames(NewDF) = c("value", "ID", "electrode")
CleanedData <- NewDF %>% group_by(electrode, ID) %>% mutate(ind = row_number()) %>% spread(electrode, value)
CleanedData$ind <- NULL
上面的代码需要data.table
和tidyverse
包才能工作,但是我要做的就是以3个数据包的形式列出和绑定您的数据(我假设您的其余数据遵循与样本数据相同的模式),并将每个“数据包”作为数据框分配给嵌套列表。然后,我将所有这些数据帧绑定在一起以创建NewDF
。
然后,我使用tidyverse
包将每个电极命名为列名,并根据值和ID进行分布。
希望这就是您想要的!