从R导出TPS文件格式

时间:2015-05-27 02:50:07

标签: r

我的数据(称为cc的对象)是一个3D数组。每个块都以一个标识符开头,后跟一个包含39行和2列的方形数据块。它们如下所示:

, , 001-10-f
           [,1]     [,2]
 [1,] 188.16863 379.5638
 [2,] 188.16863 360.3510
 [3,] 131.26338 349.7913
 [4,] 164.26256 348.1780
 [5,] 145.04971 358.0044
 [6,] 147.68964 345.8314
 [7,] 149.74292 353.1645
 [8,] 212.51469 350.5246
 [9,] 245.95385 352.2845
[10,] 229.08761 360.7910
[11,] 230.26091 348.6180
[12,] 227.91430 355.6578
[13,] 164.99587 310.3389
[14,] 211.04806 310.4856
[15,] 171.15572 320.8986
[16,] 204.88821 320.4587
[17,] 177.46223 300.2192
[18,] 199.75501 300.2192
[19,] 188.16863 303.4457
[20,] 188.16863 282.1796
[21,] 180.24883 284.8195
[22,] 199.46168 282.4729
[23,] 160.44932 265.7534
[24,] 215.44795 267.3666
[25,] 177.46223 282.9129
[26,] 205.18154 277.4864
[27,] 187.87530 257.9802
[28,] 187.87530 270.4466
[29,]  95.77094 327.0585
[30,] 282.17961 331.0184
[31,] 107.94397 254.3136
[32,] 272.93984 259.1535
[33,] 189.34193 209.2881
[34,] 113.37050 365.0442
[35,] 129.79675 389.5369
[36,] 172.62235 382.6438
[37,] 201.95495 381.4705
[38,] 244.63388 391.7369
[39,] 265.46003 369.0041

, , 002-10-f

          [,1]     [,2]
 [1,] 194.3285 361.2310
 [2,] 191.9819 344.3647
 [3,] 141.0898 340.4048
 [4,] 172.0357 336.7382
 [5,] 153.5562 345.6847
 [6,] 155.9028 335.5649
 [7,] 158.6894 342.4581
 [8,] 214.1280 333.6583
 [9,] 245.9539 333.9517
[10,] 229.9676 340.8448
[11,] 230.5542 330.1384
[12,] 228.3543 337.6182
[13,] 170.4224 293.9127
[14,] 210.7547 292.7393
[15,] 173.9423 303.1524
[16,] 208.8481 301.6858
[17,] 180.1022 286.2862
[18,] 200.3417 285.4062
[19,] 190.2219 286.4328
[20,] 188.4620 271.4732
[21,] 182.7421 274.1131
[22,] 195.3551 273.5265
[23,] 167.3425 259.1535
[24,] 213.5413 256.6602
[25,] 180.2488 272.7932
[26,] 199.6083 270.7399
[27,] 188.6086 249.6204
[28,] 189.0486 261.3535
[29,] 111.0239 321.6320
[30,] 274.9931 309.8989
[31,] 126.1302 252.1137
[32,] 259.0069 246.5405
[33,] 188.9019 202.2483
[34,] 126.4235 355.6578
[35,] 139.6232 374.4306
[36,] 182.8888 362.2576
[37,] 204.8882 359.4710
[38,] 248.8871 367.2442
[39,] 264.7267 348.4713

, , 003-10-f

           [,1]     [,2]
 [1,] 195.25311 371.5863
 [2,] 194.64767 356.9045
 [3,] 139.85572 351.1529
 [4,] 172.09518 346.4608
 [5,] 151.20764 356.9045
 [6,] 155.14298 343.8876
 [7,] 156.35385 352.5151
 [8,] 215.98929 344.6444
 [9,] 248.98555 346.6121
[10,] 235.21189 353.5746
[11,] 236.72548 341.0118
[12,] 232.63878 348.5798
[13,] 167.85713 303.3234
[14,] 214.02163 303.0207
[15,] 172.54926 314.2213
[16,] 210.23765 312.4050
[17,] 179.20906 293.7878
[18,] 204.94009 293.6365
[19,] 190.86370 293.4851
[20,] 190.10690 270.0245
[21,] 182.99303 272.9003
[22,] 199.49116 273.2030
[23,] 160.13782 261.3970
[24,] 217.80560 259.4293
[25,] 179.20906 271.8408
[26,] 204.94009 271.2353
[27,] 189.50147 248.0774
[28,] 189.65283 262.7592
[29,]  98.23199 333.7466
[30,] 283.79812 325.8759
[31,] 118.81681 250.6505
[32,] 260.64020 246.4125
[33,] 187.83652 206.3023
[34,] 119.42225 370.3755
[35,] 138.34213 386.1168
[36,] 177.99818 372.4945
[37,] 212.35668 372.3431
[38,] 247.92604 382.7869
[39,] 265.63504 367.0456

, , 004-10-f

          [,1]     [,2]
 [1,] 195.7082 358.5218
 [2,] 194.1993 340.5655
 [3,] 141.2358 329.0976
 [4,] 171.8671 329.0976
 [5,] 156.0234 336.0387
 [6,] 155.1180 324.4199
 [7,] 157.5323 331.8137
 [8,] 216.0788 326.5325
 [9,] 247.6154 326.3816
[10,] 232.3752 333.9262
[11,] 236.1475 322.6092
[12,] 232.6770 329.2485
[13,] 168.3966 284.7351
[14,] 214.2681 283.2262
[15,] 174.7341 295.2976
[16,] 209.5904 293.4869
[17,] 178.5064 275.9833
[18,] 205.0636 274.6253
[19,] 191.1814 274.3235
[20,] 191.0305 261.9502
[21,] 183.3350 264.0627
[22,] 197.8207 263.4592
[23,] 161.3046 246.2574
[24,] 218.9457 245.5029
[25,] 178.3555 261.3467
[26,] 204.6109 259.2342
[27,] 190.2761 231.6208
[28,] 190.7288 244.1449
[29,] 112.1135 310.8396
[30,] 278.0958 307.3690
[31,] 125.2412 236.9020
[32,] 262.2520 235.9967
[33,] 189.8234 191.4832
[34,] 124.0340 341.7726
[35,] 144.4046 367.4245
[36,] 180.6189 354.4477
[37,] 209.5904 355.0512
[38,] 247.7663 364.8593
[39,] 265.2699 342.2253

, , 005-10-f

          [,1]     [,2]
 [1,] 227.2118 375.5037
 [2,] 226.1675 359.3915
 [3,] 171.4159 356.2586
 [4,] 202.5959 349.5451
 [5,] 185.1411 361.1817
 [6,] 185.2903 349.3960
 [7,] 185.1411 357.1537
 [8,] 242.7272 344.4728
 [9,] 273.4598 346.2630
[10,] 254.5130 353.2748
[11,] 264.5086 341.4890
[12,] 258.3919 349.6943
[13,] 196.6285 304.3415
[14,] 244.8159 299.8659
[15,] 203.7894 316.1273
[16,] 240.3403 310.6073
[17,] 207.3699 295.8378
[18,] 235.5663 292.5557
[19,] 221.0951 292.5557
[20,] 219.4541 274.8025
[21,] 212.2931 278.5321
[22,] 226.7642 276.4435
[23,] 191.5561 266.8955
[24,] 242.2797 262.4199
[25,] 204.5354 276.2943
[26,] 232.8809 272.5646
[27,] 216.6195 247.6504
[28,] 218.1114 264.5086
[29,] 134.1191 341.0415
[30,] 298.6724 322.2439
[31,] 145.4573 264.9561
[32,] 274.2057 250.9325
[33,] 213.7850 214.2325
[34,] 155.3037 373.5642
[35,] 175.4439 387.2895
[36,] 211.9947 375.5037
[37,] 237.8041 372.6691
[38,] 267.0447 378.7858
[39,] 287.6325 364.6130

我想将此数组导出为带有以下更改的数组的.xlsx或.csv或.txt格式: 1.在每个块的标识符之前删除两个逗号(例如,在" 001-10-f&#34之前的两个逗号;),添加" IMAGE ="之前和" .JPG"在标识符之后,并从每个块的开头到结尾移动每个标识符; 2.添加" LM = 39"到每个街区的开头; 3.删除行([1,]至[39,])和列([,1]和[,2])标签。

以上更改将数据格式化为TPS格式,可用于几何形态分析。

导出的数据应如下所示:

LM=39
188.16863   379.5638
188.16863   360.351
131.26338   349.7913
164.26256   348.178
145.04971   358.0044
147.68964   345.8314
149.74292   353.1645
212.51469   350.5246
245.95385   352.2845
229.08761   360.791
230.26091   348.618
227.9143    355.6578
164.99587   310.3389
211.04806   310.4856
171.15572   320.8986
204.88821   320.4587
177.46223   300.2192
199.75501   300.2192
188.16863   303.4457
188.16863   282.1796
180.24883   284.8195
199.46168   282.4729
160.44932   265.7534
215.44795   267.3666
177.46223   282.9129
205.18154   277.4864
187.8753    257.9802
187.8753    270.4466
95.77094    327.0585
282.17961   331.0184
107.94397   254.3136
272.93984   259.1535
189.34193   209.2881
113.3705    365.0442
129.79675   389.5369
172.62235   382.6438
201.95495   381.4705
244.63388   391.7369
265.46003   369.0041
IMAGE=001-10-f.JPG
LM=39   
194.3285    361.231
191.9819    344.3647
141.0898    340.4048
172.0357    336.7382
153.5562    345.6847
155.9028    335.5649
158.6894    342.4581
214.128     333.6583
245.9539    333.9517
229.9676    340.8448
230.5542    330.1384
228.3543    337.6182
170.4224    293.9127
210.7547    292.7393
173.9423    303.1524
208.8481    301.6858
180.1022    286.2862
200.3417    285.4062
190.2219    286.4328
188.462     271.4732
182.7421    274.1131
195.3551    273.5265
167.3425    259.1535
213.5413    256.6602
180.2488    272.7932
199.6083    270.7399
188.6086    249.6204
189.0486    261.3535
111.0239    321.632
274.9931    309.8989
126.1302    252.1137
259.0069    246.5405
188.9019    202.2483
126.4235    355.6578
139.6232    374.4306
182.8888    362.2576
204.8882    359.471
248.8871    367.2442
264.7267    348.4713
IMAGE=002-10-f.JPG
LM=39   
195.25311   371.5863
194.64767   356.9045
139.85572   351.1529
172.09518   346.4608
151.20764   356.9045
155.14298   343.8876
156.35385   352.5151
215.98929   344.6444
248.98555   346.6121
235.21189   353.5746
236.72548   341.0118
232.63878   348.5798
167.85713   303.3234
214.02163   303.0207
172.54926   314.2213
210.23765   312.405
179.20906   293.7878
204.94009   293.6365
190.8637    293.4851
190.1069    270.0245
182.99303   272.9003
199.49116   273.203
160.13782   261.397
217.8056    259.4293
179.20906   271.8408
204.94009   271.2353
189.50147   248.0774
189.65283   262.7592
98.23199    333.7466
283.79812   325.8759
118.81681   250.6505
260.6402    246.4125
187.83652   206.3023
119.42225   370.3755
138.34213   386.1168
177.99818   372.4945
212.35668   372.3431
247.92604   382.7869
265.63504   367.0456
IMAGE=003-10-f.JPG
LM=39   
195.7082    358.5218
194.1993    340.5655
141.2358    329.0976
171.8671    329.0976
156.0234    336.0387
155.118     324.4199
157.5323    331.8137
216.0788    326.5325
247.6154    326.3816
232.3752    333.9262
236.1475    322.6092
232.677     329.2485
168.3966    284.7351
214.2681    283.2262
174.7341    295.2976
209.5904    293.4869
178.5064    275.9833
205.0636    274.6253
191.1814    274.3235
191.0305    261.9502
183.335     264.0627
197.8207    263.4592
161.3046    246.2574
218.9457    245.5029
178.3555    261.3467
204.6109    259.2342
190.2761    231.6208
190.7288    244.1449
112.1135    310.8396
278.0958    307.369
125.2412    236.902
262.252     235.9967
189.8234    191.4832
124.034     341.7726
144.4046    367.4245
180.6189    354.4477
209.5904    355.0512
247.7663    364.8593
265.2699    342.2253
IMAGE=004-10-f.JPG
LM=39   
227.2118    375.5037
226.1675    359.3915
171.4159    356.2586
202.5959    349.5451
185.1411    361.1817
185.2903    349.396
185.1411    357.1537
242.7272    344.4728
273.4598    346.263
254.513     353.2748
264.5086    341.489
258.3919    349.6943
196.6285    304.3415
244.8159    299.8659
203.7894    316.1273
240.3403    310.6073
207.3699    295.8378
235.5663    292.5557
221.0951    292.5557
219.4541    274.8025
212.2931    278.5321
226.7642    276.4435
191.5561    266.8955
242.2797    262.4199
204.5354    276.2943
232.8809    272.5646
216.6195    247.6504
218.1114    264.5086
134.1191    341.0415
298.6724    322.2439
145.4573    264.9561
274.2057    250.9325
213.785     214.2325
155.3037    373.5642
175.4439    387.2895
211.9947    375.5037
237.8041    372.6691
267.0447    378.7858
287.6325    364.613
IMAGE=005-10-f.JPG

我可以知道如何在R中执行转换并导出转换后的对象吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

您必须迭代cc并根据需要使用字符串格式。您可以使用嵌套的for循环和R的sprintf版本。

sink ('output-file.txt')

for (i in 1:dim(cc)[3]) {
  cat (sprintf ("LM=39\n"))
  for (j in 1:dim(cc)[1]) {
    cat (sprintf ("%.4f    %.4f\n", cc[j,1,i], cc[j,2,i]))
  }
  cat (sprintf ("IMAGE=%s.JPG\n", dimnames(cc)[[3]][i]))  
}