如何在文本文件中编写列表列表

时间:2016-11-01 08:29:33

标签: r list

我有兴趣在文本文件中以相同的模式编写列表列表。我想写的数据显示

structure(list(alpha = structure(list(coord = structure(c(-12.2866476198535, 
-18.316117409566, -8.967429617903, 1.12428419143426, 5.64841344065713, 
5.44808832719262, 5.4933681771463, 1.9309844060162, 0.233398747299152, 
-2.99825174446503, -0.614368425069781, 2.46564289448005, 4.66090502549971, 
5.85072710241625, 6.05039548299675, 3.76632919408699, 1.41357656882162, 
-0.903298741189705, -2.04803182906018, -4.29159971822507, -10.9370577335933, 
-10.4408842089726, -3.96875616934913, 0.121960788365283, 6.18374509958836, 
8.14853675618862, 9.88793476169869, 3.88115658226667, -2.54678957335455, 
-6.47283540567184, -4.77245571184901, -0.627696099407765, 1.88975367842293, 
3.5575754911426, 6.77903556061373, 5.65640773119659), .Dim = c(18L, 
2L), .Dimnames = list(c("alpha0", "alpha7", "alpha14", "alpha21", 
"alpha28", "alpha35", "alpha42", "alpha49", "alpha56", "alpha63", 
"alpha70", "alpha77", "alpha84", "alpha91", "alpha98", "alpha105", 
"alpha112", "alpha119"), NULL)), totalVar = 224.006168211038), .Names = c("coord", 
"totalVar")), cdc15 = structure(list(coord = structure(c(3.47162630576928, 
8.48782177704198, 15.0137638851927, 10.7906288581053, 3.63385217951696, 
5.35572208423149, -3.25353199133528, -1.56135110102186, -7.75612902660608, 
0.336703958433859, 0.369082417850372, 5.34503555916275, 1.92967854766031, 
6.51852381743397, -1.21720901946083, 4.5170731102192, -3.30408507300723, 
-0.984750968271877, -8.40102749295709, -8.46082456852006, -12.9415452444904, 
-5.24176034595145, -6.55825327910539, -6.08904438989054, -1.86350110076595, 
-11.4352897317111, -7.97410132086123, 4.0084226414636, 5.73268492795077, 
9.73019959676426, 8.39274636758967, 9.64829450668746, 4.37500530928532, 
-4.46583186497905, -7.38893631815797, -7.96186328098463, -5.8246279470231, 
-3.7664732488773, -0.197403598498376, 5.49542607598301, 4.32441586772294, 
6.97775742830444, 4.22579465801243, 1.89421538493436, -2.49167768466654, 
-4.00933035006213, -4.2725035868669, -3.15342273124391), .Dim = c(24L, 
2L), .Dimnames = list(c("cdc15_10", "cdc15_30", "cdc15_50", "cdc15_70", 
"cdc15_80", "cdc15_90", "cdc15_100", "cdc15_110", "cdc15_120", 
"cdc15_130", "cdc15_140", "cdc15_150", "cdc15_160", "cdc15_170", 
"cdc15_180", "cdc15_190", "cdc15_200", "cdc15_210", "cdc15_220", 
"cdc15_230", "cdc15_240", "cdc15_250", "cdc15_270", "cdc15_290"
), NULL)), totalVar = 465.172014273611), .Names = c("coord", 
"totalVar")), cdc28 = structure(list(coord = structure(c(-17.0546131306391, 
-3.66229994382873, 11.6887094745458, 9.1742388638346, 8.52829507051842, 
5.07577055316834, 4.08395116454314, -0.173829127948164, -4.55958318577516, 
-2.26498682123389, -1.96917640175427, 1.2139570540714, 0.949348894924083, 
1.42543768864397, -1.34172880845612, -4.09831002297555, -7.01518132163873, 
-6.11841279145655, -7.46318485493823, -11.0977474794828, -6.5158721204813, 
0.100407193168119, 2.48089061979771, 6.88321073830055, 8.8512526398517, 
6.44139467778423, -1.4938717542132, -6.27905863623537, -4.7271008527727, 
-1.28540378171629, 2.52270971781573, 5.39065653580633, 7.37698285416012, 
4.933147294612), .Dim = c(17L, 2L), .Dimnames = list(c("cdc28_0", 
"cdc28_10", "cdc28_20", "cdc28_30", "cdc28_40", "cdc28_50", "cdc28_60", 
"cdc28_70", "cdc28_80", "cdc28_90", "cdc28_100", "cdc28_110", 
"cdc28_120", "cdc28_130", "cdc28_140", "cdc28_150", "cdc28_160"
), NULL)), totalVar = 434.213382002418), .Names = c("coord", 
"totalVar")), elu = structure(list(coord = structure(c(-16.5385661891214, 
-7.22079482875697, -3.54426033968934, -0.490137067585021, 6.41505042855706, 
8.5763425812589, 7.09765646432215, 5.08639180593248, 2.03913603133563, 
4.82068185798214, 3.3960214921007, -2.08018553751718, -4.74563686297203, 
-2.81169983584704, -6.14568798086542, 1.07761816321543, -3.65016036562603, 
-6.63747020746656, -8.38502803731479, -6.58956439536117, -2.03845851348116, 
3.12479766419619, 7.8140081835477, 8.65512130826726, 7.31933446871695, 
6.30983959657581, 4.23861782098735, -5.09296770539154), .Dim = c(14L, 
2L), .Dimnames = list(c("elu0", "elu30", "elu60", "elu90", "elu120", 
"elu150", "elu180", "elu210", "elu240", "elu270", "elu300", "elu330", 
"elu360", "elu390"), NULL)), totalVar = 264.546605949057), .Names = c("coord", 
"totalVar"))), .Names = c("alpha", "cdc15", "cdc28", "elu"))

上面给出的数据应写入文本文件(列表列表)。这该怎么做 ??

已修改 txt / csv格式的预期输出

$alpha
$alpha$coord
                [,1]        [,2]
alpha0   -12.2866476  -2.0480318
alpha7   -18.3161174  -4.2915997
alpha14   -8.9674296 -10.9370577
alpha21    1.1242842 -10.4408842
alpha28    5.6484134  -3.9687562
alpha35    5.4480883   0.1219608
alpha42    5.4933682   6.1837451
alpha49    1.9309844   8.1485368
alpha56    0.2333987   9.8879348
alpha63   -2.9982517   3.8811566
alpha70   -0.6143684  -2.5467896
alpha77    2.4656429  -6.4728354
alpha84    4.6609050  -4.7724557
alpha91    5.8507271  -0.6276961
alpha98    6.0503955   1.8897537
alpha105   3.7663292   3.5575755
alpha112   1.4135766   6.7790356
alpha119  -0.9032987   5.6564077

$alpha$totalVar
[1] 224.0062


$cdc15
$cdc15$coord
                 [,1]        [,2]
cdc15_10    3.4716263  -1.8635011
cdc15_30    8.4878218 -11.4352897
cdc15_50   15.0137639  -7.9741013
cdc15_70   10.7906289   4.0084226
cdc15_80    3.6338522   5.7326849
cdc15_90    5.3557221   9.7301996
cdc15_100  -3.2535320   8.3927464
cdc15_110  -1.5613511   9.6482945
cdc15_120  -7.7561290   4.3750053
cdc15_130   0.3367040  -4.4658319
cdc15_140   0.3690824  -7.3889363
cdc15_150   5.3450356  -7.9618633
cdc15_160   1.9296785  -5.8246279
cdc15_170   6.5185238  -3.7664732
cdc15_180  -1.2172090  -0.1974036
cdc15_190   4.5170731   5.4954261
cdc15_200  -3.3040851   4.3244159
cdc15_210  -0.9847510   6.9777574
cdc15_220  -8.4010275   4.2257947
cdc15_230  -8.4608246   1.8942154
cdc15_240 -12.9415452  -2.4916777
cdc15_250  -5.2417603  -4.0093304
cdc15_270  -6.5582533  -4.2725036
cdc15_290  -6.0890444  -3.1534227

$cdc15$totalVar
[1] 465.172


$cdc28
$cdc28$coord
                 [,1]        [,2]
cdc28_0   -17.0546131  -6.1184128
cdc28_10   -3.6622999  -7.4631849
cdc28_20   11.6887095 -11.0977475
cdc28_30    9.1742389  -6.5158721
cdc28_40    8.5282951   0.1004072
cdc28_50    5.0757706   2.4808906
cdc28_60    4.0839512   6.8832107
cdc28_70   -0.1738291   8.8512526
cdc28_80   -4.5595832   6.4413947
cdc28_90   -2.2649868  -1.4938718
cdc28_100  -1.9691764  -6.2790586
cdc28_110   1.2139571  -4.7271009
cdc28_120   0.9493489  -1.2854038
cdc28_130   1.4254377   2.5227097
cdc28_140  -1.3417288   5.3906565
cdc28_150  -4.0983100   7.3769829
cdc28_160  -7.0151813   4.9331473

$cdc28$totalVar
[1] 434.2134


$elu
$elu$coord
              [,1]      [,2]
elu0   -16.5385662 -6.145688
elu30   -7.2207948  1.077618
elu60   -3.5442603 -3.650160
elu90   -0.4901371 -6.637470
elu120   6.4150504 -8.385028
elu150   8.5763426 -6.589564
elu180   7.0976565 -2.038459
elu210   5.0863918  3.124798
elu240   2.0391360  7.814008
elu270   4.8206819  8.655121
elu300   3.3960215  7.319334
elu330  -2.0801855  6.309840
elu360  -4.7456369  4.238618
elu390  -2.8116998 -5.092968

$elu$totalVar
[1] 264.5466

1 个答案:

答案 0 :(得分:1)

虽然您可以合并writeLinescapture.output,但在这些情况下我更喜欢sink函数,它只是将R输出转移到文件或连接。所以,你应该得到你想要的东西:

sink("myfile.txt")
print(mylist)
sink()

其中mylist是您的列表,"myfile.txt"是您写入的文本文件(包括路径)。