Issues with converting data into a matrix after running lapply() - r

I am using the following bit of code to load data into R:
filelist <- list.files(pattern = "^KB.*.txt")
sorted <- mixedsort(sort(filelist))
#sorts the data in numerical order (i.e. c1-c11, fixed)
data_list = lapply(sorted, read.table, sep = "")
#loads all the .txt files into R
This is the output of sorted
[1] "KB_5223_LLM1_rotated_1.jpg_c1.txt"
[2] "KB_5223_LLM1_rotated_1.jpg_c2.txt"
[3] "KB_5223_LLM1_rotated_1.jpg_c3.txt"
[4] "KB_5223_LLM1_rotated_1.jpg_c4.txt"
[5] "KB_5223_LLM1_rotated_1.jpg_c5.txt"
[6] "KB_5223_LLM1_rotated_1.jpg_c6.txt"
[7] "KB_5223_LLM1_rotated_1.jpg_c7.txt"
[8] "KB_5223_LLM1_rotated_1.jpg_c8.txt"
[9] "KB_5223_LLM1_rotated_1.jpg_c9.txt"
[10] "KB_5223_LLM1_rotated_1.jpg_c10.txt"
[11] "KB_5223_LLM1_rotated_1.jpg_c11.txt"
[12] "KB_5223_LLM1_rotated_1.jpg_fixed.txt"
However, the data looks like (these are only 2 of the 12 tables, as the data is very large)
[[10]]
V1 V2
1 12.1153 6.3112
2 12.0841 6.2956
3 12.0529 6.2800
4 12.0217 6.2644
5 11.9906 6.2488
6 11.9595 6.2332
7 11.9285 6.2176
8 11.8976 6.2019
9 11.8668 6.1862
10 11.8362 6.1705
11 11.8056 6.1547
12 11.7753 6.1389
13 11.7451 6.1231
14 11.7151 6.1071
15 11.6854 6.0912
16 11.6558 6.0751
17 11.6265 6.0590
18 11.5974 6.0428
19 11.5686 6.0266
20 11.5401 6.0103
21 11.5118 5.9940
22 11.4837 5.9776
23 11.4558 5.9613
24 11.4282 5.9450
25 11.4008 5.9288
26 11.3736 5.9127
27 11.3465 5.8966
28 11.3197 5.8807
29 11.2930 5.8649
30 11.2664 5.8493
31 11.2400 5.8338
32 11.2138 5.8186
33 11.1877 5.8036
34 11.1617 5.7888
35 11.1358 5.7741
36 11.1101 5.7596
37 11.0844 5.7450
38 11.0588 5.7305
39 11.0334 5.7158
40 11.0080 5.7009
41 10.9827 5.6857
42 10.9574 5.6702
43 10.9323 5.6542
44 10.9071 5.6378
45 10.8821 5.6208
46 10.8570 5.6031
47 10.8320 5.5848
48 10.8071 5.5657
49 10.7822 5.5458
50 10.7572 5.5252
51 10.7323 5.5041
52 10.7074 5.4824
53 10.6825 5.4604
54 10.6576 5.4380
55 10.6327 5.4154
56 10.6077 5.3927
57 10.5827 5.3699
58 10.5577 5.3472
59 10.5326 5.3246
60 10.5075 5.3022
61 10.4823 5.2801
62 10.4570 5.2584
63 10.4317 5.2372
64 10.4063 5.2166
65 10.3808 5.1966
66 10.3552 5.1772
67 10.3296 5.1583
68 10.3039 5.1401
69 10.2781 5.1224
70 10.2523 5.1053
71 10.2264 5.0887
72 10.2004 5.0726
73 10.1744 5.0571
74 10.1484 5.0421
75 10.1223 5.0275
76 10.0961 5.0135
77 10.0699 4.9999
78 10.0437 4.9868
79 10.0175 4.9741
80 9.9912 4.9619
81 9.9648 4.9500
82 9.9383 4.9385
83 9.9116 4.9272
84 9.8847 4.9163
85 9.8575 4.9055
86 9.8301 4.8950
87 9.8023 4.8846
88 9.7741 4.8743
89 9.7456 4.8640
90 9.7166 4.8538
91 9.6871 4.8436
92 9.6571 4.8334
93 9.6265 4.8230
94 9.5953 4.8126
95 9.5636 4.8021
96 9.5314 4.7916
97 9.4988 4.7813
98 9.4658 4.7711
99 9.4324 4.7611
100 9.3987 4.7513
101 9.3648 4.7420
102 9.3307 4.7330
103 9.2965 4.7245
104 9.2621 4.7166
105 9.2277 4.7092
106 9.1933 4.7025
107 9.1589 4.6966
108 9.1246 4.6914
109 9.0905 4.6871
110 9.0566 4.6837
111 9.0228 4.6812
112 8.9892 4.6795
113 8.9558 4.6784
114 8.9225 4.6778
115 8.8893 4.6777
116 8.8561 4.6778
117 8.8230 4.6780
118 8.7898 4.6783
119 8.7567 4.6785
120 8.7234 4.6784
121 8.6901 4.6780
122 8.6567 4.6772
123 8.6232 4.6757
124 8.5894 4.6735
125 8.5555 4.6705
126 8.5214 4.6665
127 8.4870 4.6615
128 8.4525 4.6556
129 8.4178 4.6488
130 8.3831 4.6414
131 8.3484 4.6332
132 8.3138 4.6246
133 8.2793 4.6156
134 8.2450 4.6062
135 8.2110 4.5966
136 8.1772 4.5868
137 8.1438 4.5770
138 8.1109 4.5673
139 8.0784 4.5578
140 8.0465 4.5486
141 8.0152 4.5397
142 7.9845 4.5313
143 7.9546 4.5234
144 7.9253 4.5161
145 7.8965 4.5093
146 7.8681 4.5028
147 7.8398 4.4966
148 7.8116 4.4906
149 7.7832 4.4846
150 7.7546 4.4786
151 7.7256 4.4724
152 7.6959 4.4661
153 7.6655 4.4594
154 7.6343 4.4522
155 7.6019 4.4446
156 7.5684 4.4363
157 7.5334 4.4273
158 7.4970 4.4175
159 7.4590 4.4068
160 7.4195 4.3954
161 7.3785 4.3831
162 7.3362 4.3702
163 7.2926 4.3565
164 7.2477 4.3421
165 7.2017 4.3271
166 7.1545 4.3115
167 7.1063 4.2953
168 7.0571 4.2786
169 7.0069 4.2614
170 6.9558 4.2437
171 6.9040 4.2255
172 6.8514 4.2070
173 6.7981 4.1881
174 6.7442 4.1688
175 6.6897 4.1493
176 6.6347 4.1295
177 6.5793 4.1094
178 6.5236 4.0892
179 6.4675 4.0687
180 6.4111 4.0482
181 6.3546 4.0275
182 6.2979 4.0068
183 6.2412 3.9860
[[11]]
V1 V2
1 12.1153 6.3112
2 12.1376 6.2977
3 12.1599 6.2842
4 12.1821 6.2705
5 12.2041 6.2567
6 12.2259 6.2426
7 12.2476 6.2281
8 12.2689 6.2133
9 12.2900 6.1980
10 12.3107 6.1822
11 12.3309 6.1658
12 12.3508 6.1488
13 12.3701 6.1311
14 12.3889 6.1125
15 12.4071 6.0932
16 12.4247 6.0729
17 12.4416 6.0517
18 12.4578 6.0295
19 12.4734 6.0064
20 12.4885 5.9825
21 12.5030 5.9579
22 12.5170 5.9326
23 12.5305 5.9067
24 12.5437 5.8802
25 12.5565 5.8533
26 12.5689 5.8260
27 12.5811 5.7983
28 12.5930 5.7703
29 12.6047 5.7421
30 12.6163 5.7138
31 12.6278 5.6855
32 12.6391 5.6571
33 12.6505 5.6287
34 12.6618 5.6006
35 12.6732 5.5725
36 12.6846 5.5447
37 12.6961 5.5170
38 12.7077 5.4895
39 12.7194 5.4622
40 12.7311 5.4351
41 12.7430 5.4082
42 12.7550 5.3815
43 12.7671 5.3551
44 12.7794 5.3288
45 12.7918 5.3028
46 12.8044 5.2769
47 12.8172 5.2513
48 12.8302 5.2260
49 12.8434 5.2008
50 12.8568 5.1760
51 12.8704 5.1513
52 12.8843 5.1269
53 12.8983 5.1027
54 12.9126 5.0787
55 12.9270 5.0547
56 12.9415 5.0308
57 12.9561 5.0068
58 12.9709 4.9828
59 12.9856 4.9587
60 13.0004 4.9344
61 13.0152 4.9099
62 13.0300 4.8851
63 13.0447 4.8601
64 13.0593 4.8346
65 13.0739 4.8088
66 13.0883 4.7825
67 13.1025 4.7557
68 13.1166 4.7283
69 13.1305 4.7003
70 13.1442 4.6719
71 13.1576 4.6430
72 13.1709 4.6137
73 13.1840 4.5842
74 13.1969 4.5544
75 13.2096 4.5244
76 13.2221 4.4943
77 13.2344 4.4642
78 13.2465 4.4341
79 13.2584 4.4041
80 13.2702 4.3743
81 13.2817 4.3447
82 13.2930 4.3154
83 13.3042 4.2864
84 13.3151 4.2579
85 13.3259 4.2299
86 13.3365 4.2025
87 13.3469 4.1757
88 13.3571 4.1494
89 13.3671 4.1238
90 13.3769 4.0986
91 13.3866 4.0739
92 13.3961 4.0495
93 13.4055 4.0255
94 13.4147 4.0018
95 13.4237 3.9783
96 13.4326 3.9550
97 13.4414 3.9318
98 13.4501 3.9087
99 13.4586 3.8856
100 13.4670 3.8624
101 13.4753 3.8391
102 13.4834 3.8157
103 13.4916 3.7920
104 13.4996 3.7681
105 13.5077 3.7438
106 13.5159 3.7191
107 13.5242 3.6940
108 13.5326 3.6682
109 13.5413 3.6419
110 13.5503 3.6148
111 13.5595 3.5870
112 13.5691 3.5584
113 13.5792 3.5289
114 13.5897 3.4984
115 13.6007 3.4669
116 13.6123 3.4343
117 13.6245 3.4006
118 13.6373 3.3656
119 13.6509 3.3293
120 13.6652 3.2917
121 13.6803 3.2527
122 13.6961 3.2124
123 13.7126 3.1708
124 13.7297 3.1280
125 13.7476 3.0841
126 13.7661 3.0390
127 13.7852 2.9928
128 13.8049 2.9455
129 13.8251 2.8972
130 13.8459 2.8480
131 13.8673 2.7978
132 13.8891 2.7467
133 13.9114 2.6947
134 13.9342 2.6419
135 13.9574 2.5884
136 13.9810 2.5341
137 14.0050 2.4791
138 14.0293 2.4235
139 14.0540 2.3672
140 14.0791 2.3104
141 14.1044 2.2531
142 14.1300 2.1953
143 14.1558 2.1370
144 14.1819 2.0783
145 14.2081 2.0192
146 14.2346 1.9598
147 14.2612 1.9002
148 14.2880 1.8403
149 14.3148 1.7801
150 14.3418 1.7199
151 14.3688 1.6595
152 14.3959 1.5990
153 14.4230 1.5385
after loading it up, but I need to it be a matrix and look like
V1 V2
[1,] 12.1153 6.3112
[2,] 12.0841 6.2956
[3,] 12.0529 6.2800
[4,] 12.0217 6.2644
[5,] 11.9906 6.2488
[6,] 11.9595 6.2332
[7,] 11.9285 6.2176
[8,] 11.8976 6.2019
[9,] 11.8668 6.1862
[10,] 11.8362 6.1705
[11,] 11.8056 6.1547
[12,] 11.7753 6.1389
[13,] 11.7451 6.1231
[14,] 11.7151 6.1071
[15,] 11.6854 6.0912
[16,] 11.6558 6.0751
[17,] 11.6265 6.0590
[18,] 11.5974 6.0428
[19,] 11.5686 6.0266
[20,] 11.5401 6.0103
[21,] 11.5118 5.9940
[22,] 11.4837 5.9776
[23,] 11.4558 5.9613
[24,] 11.4282 5.9450
[25,] 11.4008 5.9288
[26,] 11.3736 5.9127
[27,] 11.3465 5.8966
[28,] 11.3197 5.8807
[29,] 11.2930 5.8649
[30,] 11.2664 5.8493
[31,] 11.2400 5.8338
[32,] 11.2138 5.8186
[33,] 11.1877 5.8036
[34,] 11.1617 5.7888
[35,] 11.1358 5.7741
[36,] 11.1101 5.7596
[37,] 11.0844 5.7450
[38,] 11.0588 5.7305
[39,] 11.0334 5.7158
[40,] 11.0080 5.7009
[41,] 10.9827 5.6857
[42,] 10.9574 5.6702
[43,] 10.9323 5.6542
[44,] 10.9071 5.6378
[45,] 10.8821 5.6208
[46,] 10.8570 5.6031
[47,] 10.8320 5.5848
[48,] 10.8071 5.5657
[49,] 10.7822 5.5458
[50,] 10.7572 5.5252
[51,] 10.7323 5.5041
[52,] 10.7074 5.4824
[53,] 10.6825 5.4604
[54,] 10.6576 5.4380
[55,] 10.6327 5.4154
[56,] 10.6077 5.3927
[57,] 10.5827 5.3699
[58,] 10.5577 5.3472
[59,] 10.5326 5.3246
[60,] 10.5075 5.3022
[61,] 10.4823 5.2801
[62,] 10.4570 5.2584
[63,] 10.4317 5.2372
[64,] 10.4063 5.2166
[65,] 10.3808 5.1966
[66,] 10.3552 5.1772
[67,] 10.3296 5.1583
[68,] 10.3039 5.1401
[69,] 10.2781 5.1224
[70,] 10.2523 5.1053
[71,] 10.2264 5.0887
[72,] 10.2004 5.0726
[73,] 10.1744 5.0571
[74,] 10.1484 5.0421
[75,] 10.1223 5.0275
[76,] 10.0961 5.0135
[77,] 10.0699 4.9999
[78,] 10.0437 4.9868
[79,] 10.0175 4.9741
[80,] 9.9912 4.9619
[81,] 9.9648 4.9500
[82,] 9.9383 4.9385
[83,] 9.9116 4.9272
[84,] 9.8847 4.9163
[85,] 9.8575 4.9055
[86,] 9.8301 4.8950
[87,] 9.8023 4.8846
[88,] 9.7741 4.8743
[89,] 9.7456 4.8640
[90,] 9.7166 4.8538
[91,] 9.6871 4.8436
[92,] 9.6571 4.8334
[93,] 9.6265 4.8230
[94,] 9.5953 4.8126
[95,] 9.5636 4.8021
[96,] 9.5314 4.7916
[97,] 9.4988 4.7813
[98,] 9.4658 4.7711
[99,] 9.4324 4.7611
[100,] 9.3987 4.7513
[101,] 9.3648 4.7420
[102,] 9.3307 4.7330
[103,] 9.2965 4.7245
[104,] 9.2621 4.7166
[105,] 9.2277 4.7092
[106,] 9.1933 4.7025
[107,] 9.1589 4.6966
[108,] 9.1246 4.6914
[109,] 9.0905 4.6871
[110,] 9.0566 4.6837
[111,] 9.0228 4.6812
[112,] 8.9892 4.6795
[113,] 8.9558 4.6784
[114,] 8.9225 4.6778
[115,] 8.8893 4.6777
[116,] 8.8561 4.6778
[117,] 8.8230 4.6780
[118,] 8.7898 4.6783
[119,] 8.7567 4.6785
[120,] 8.7234 4.6784
[121,] 8.6901 4.6780
[122,] 8.6567 4.6772
[123,] 8.6232 4.6757
[124,] 8.5894 4.6735
[125,] 8.5555 4.6705
[126,] 8.5214 4.6665
[127,] 8.4870 4.6615
[128,] 8.4525 4.6556
[129,] 8.4178 4.6488
[130,] 8.3831 4.6414
[131,] 8.3484 4.6332
[132,] 8.3138 4.6246
[133,] 8.2793 4.6156
[134,] 8.2450 4.6062
[135,] 8.2110 4.5966
[136,] 8.1772 4.5868
[137,] 8.1438 4.5770
[138,] 8.1109 4.5673
[139,] 8.0784 4.5578
[140,] 8.0465 4.5486
[141,] 8.0152 4.5397
[142,] 7.9845 4.5313
[143,] 7.9546 4.5234
[144,] 7.9253 4.5161
[145,] 7.8965 4.5093
[146,] 7.8681 4.5028
[147,] 7.8398 4.4966
[148,] 7.8116 4.4906
[149,] 7.7832 4.4846
[150,] 7.7546 4.4786
[151,] 7.7256 4.4724
[152,] 7.6959 4.4661
[153,] 7.6655 4.4594
[154,] 7.6343 4.4522
[155,] 7.6019 4.4446
[156,] 7.5684 4.4363
[157,] 7.5334 4.4273
[158,] 7.4970 4.4175
[159,] 7.4590 4.4068
[160,] 7.4195 4.3954
[161,] 7.3785 4.3831
[162,] 7.3362 4.3702
[163,] 7.2926 4.3565
[164,] 7.2477 4.3421
[165,] 7.2017 4.3271
[166,] 7.1545 4.3115
[167,] 7.1063 4.2953
[168,] 7.0571 4.2786
[169,] 7.0069 4.2614
[170,] 6.9558 4.2437
[171,] 6.9040 4.2255
[172,] 6.8514 4.2070
[173,] 6.7981 4.1881
[174,] 6.7442 4.1688
[175,] 6.6897 4.1493
[176,] 6.6347 4.1295
[177,] 6.5793 4.1094
[178,] 6.5236 4.0892
[179,] 6.4675 4.0687
[180,] 6.4111 4.0482
[181,] 6.3546 4.0275
[182,] 6.2979 4.0068
[183,] 6.2412 3.9860
V1 V2
[1,] 12.1153 6.3112
[2,] 12.1376 6.2977
[3,] 12.1599 6.2842
[4,] 12.1821 6.2705
[5,] 12.2041 6.2567
[6,] 12.2259 6.2426
[7,] 12.2476 6.2281
[8,] 12.2689 6.2133
[9,] 12.2900 6.1980
[10,] 12.3107 6.1822
[11,] 12.3309 6.1658
[12,] 12.3508 6.1488
[13,] 12.3701 6.1311
[14,] 12.3889 6.1125
[15,] 12.4071 6.0932
[16,] 12.4247 6.0729
[17,] 12.4416 6.0517
[18,] 12.4578 6.0295
[19,] 12.4734 6.0064
[20,] 12.4885 5.9825
[21,] 12.5030 5.9579
[22,] 12.5170 5.9326
[23,] 12.5305 5.9067
[24,] 12.5437 5.8802
[25,] 12.5565 5.8533
[26,] 12.5689 5.8260
[27,] 12.5811 5.7983
[28,] 12.5930 5.7703
[29,] 12.6047 5.7421
[30,] 12.6163 5.7138
[31,] 12.6278 5.6855
[32,] 12.6391 5.6571
[33,] 12.6505 5.6287
[34,] 12.6618 5.6006
[35,] 12.6732 5.5725
[36,] 12.6846 5.5447
[37,] 12.6961 5.5170
[38,] 12.7077 5.4895
[39,] 12.7194 5.4622
[40,] 12.7311 5.4351
[41,] 12.7430 5.4082
[42,] 12.7550 5.3815
[43,] 12.7671 5.3551
[44,] 12.7794 5.3288
[45,] 12.7918 5.3028
[46,] 12.8044 5.2769
[47,] 12.8172 5.2513
[48,] 12.8302 5.2260
[49,] 12.8434 5.2008
[50,] 12.8568 5.1760
[51,] 12.8704 5.1513
[52,] 12.8843 5.1269
[53,] 12.8983 5.1027
[54,] 12.9126 5.0787
[55,] 12.9270 5.0547
[56,] 12.9415 5.0308
[57,] 12.9561 5.0068
[58,] 12.9709 4.9828
[59,] 12.9856 4.9587
[60,] 13.0004 4.9344
[61,] 13.0152 4.9099
[62,] 13.0300 4.8851
[63,] 13.0447 4.8601
[64,] 13.0593 4.8346
[65,] 13.0739 4.8088
[66,] 13.0883 4.7825
[67,] 13.1025 4.7557
[68,] 13.1166 4.7283
[69,] 13.1305 4.7003
[70,] 13.1442 4.6719
[71,] 13.1576 4.6430
[72,] 13.1709 4.6137
[73,] 13.1840 4.5842
[74,] 13.1969 4.5544
[75,] 13.2096 4.5244
[76,] 13.2221 4.4943
[77,] 13.2344 4.4642
[78,] 13.2465 4.4341
[79,] 13.2584 4.4041
[80,] 13.2702 4.3743
[81,] 13.2817 4.3447
[82,] 13.2930 4.3154
[83,] 13.3042 4.2864
[84,] 13.3151 4.2579
[85,] 13.3259 4.2299
[86,] 13.3365 4.2025
[87,] 13.3469 4.1757
[88,] 13.3571 4.1494
[89,] 13.3671 4.1238
[90,] 13.3769 4.0986
[91,] 13.3866 4.0739
[92,] 13.3961 4.0495
[93,] 13.4055 4.0255
[94,] 13.4147 4.0018
[95,] 13.4237 3.9783
[96,] 13.4326 3.9550
[97,] 13.4414 3.9318
[98,] 13.4501 3.9087
[99,] 13.4586 3.8856
[100,] 13.4670 3.8624
[101,] 13.4753 3.8391
[102,] 13.4834 3.8157
[103,] 13.4916 3.7920
[104,] 13.4996 3.7681
[105,] 13.5077 3.7438
[106,] 13.5159 3.7191
[107,] 13.5242 3.6940
[108,] 13.5326 3.6682
[109,] 13.5413 3.6419
[110,] 13.5503 3.6148
[111,] 13.5595 3.5870
[112,] 13.5691 3.5584
[113,] 13.5792 3.5289
[114,] 13.5897 3.4984
[115,] 13.6007 3.4669
[116,] 13.6123 3.4343
[117,] 13.6245 3.4006
[118,] 13.6373 3.3656
[119,] 13.6509 3.3293
[120,] 13.6652 3.2917
[121,] 13.6803 3.2527
[122,] 13.6961 3.2124
[123,] 13.7126 3.1708
[124,] 13.7297 3.1280
[125,] 13.7476 3.0841
[126,] 13.7661 3.0390
[127,] 13.7852 2.9928
[128,] 13.8049 2.9455
[129,] 13.8251 2.8972
[130,] 13.8459 2.8480
[131,] 13.8673 2.7978
[132,] 13.8891 2.7467
[133,] 13.9114 2.6947
[134,] 13.9342 2.6419
[135,] 13.9574 2.5884
[136,] 13.9810 2.5341
[137,] 14.0050 2.4791
[138,] 14.0293 2.4235
[139,] 14.0540 2.3672
[140,] 14.0791 2.3104
[141,] 14.1044 2.2531
[142,] 14.1300 2.1953
[143,] 14.1558 2.1370
[144,] 14.1819 2.0783
[145,] 14.2081 2.0192
[146,] 14.2346 1.9598
[147,] 14.2612 1.9002
[148,] 14.2880 1.8403
[149,] 14.3148 1.7801
[150,] 14.3418 1.7199
[151,] 14.3688 1.6595
[152,] 14.3959 1.5990
[153,] 14.4230 1.5385
I am aware of the as.matrix(read.land())function, and the simple as.matrix() but I can't get it to work on my data_list. I feel like this might stem from me not fully understanding what exactly lapply() does and what sort of data it produces.
I would be grateful for any help.
Thanks.

If you want to store each element of your data_list as an object in your global environment, you can first make your list of data.frame a named list, then us list2env to convert the elements to separate data.frame's in your environment:
names(data_list) = c(paste0("c", 1:11), "fixed")
list2env(data_list, envir = .GlobalEnv)
Depending on your use case though, you might want to keep the data.frames in a list, as it seems like the data.frames are somewhat related. You can easily iterate over elements of a list, but not so much on separate data.frame objects in your global environment (especially if your data.frames have very different names). For example, #clemens's answer combines all data.frames in data_list. Not so easy to do that if all your data.frames are in separate objects.

You can wrap rbindlist() from the data.table package around your lapply() (which is the same as do.call("rbind", data_list), but faster):
library(data.table)
data_list = rbindlist(lapply(sorted, read.table, sep = ""))
If you want to create a dataframe for each file, you could try:
lapply(sorted, function(x) {
assign(x, read.table(x, sep = ""),
envir = .GlobalEnv)
})

Related

How do I calculate perimeter of xy points in R?

I am looking to calculate the perimeter of xy points (including all the points in sequential order), where the first and last point aren't always equal. I can't figure out why the code below for calculating perimeter isn't working.
Ideally, I would like a method which flags when the first and last xy points are different.
Thanks
m
x y
[1,] 606.3 95.4
[2,] 612.4 178.7
[3,] 610.2 222.6
[4,] 610.2 222.8
[5,] 625.8 249.8
[6,] 625.8 250.1
[7,] 633.9 268.9
[8,] 668.7 272.2
[9,] 693.7 222.6
[10,] 723.2 157.0
[11,] 738.6 109.9
[12,] 681.2 90.5
[13,] 606.3 95.4
[14,] 833.3 154.6
[15,] 753.7 267.5
[16,] 747.8 305.1
[17,] 773.8 354.7
[18,] 767.0 393.8
[19,] 763.0 442.0
[20,] 817.4 446.9
[21,] 817.6 446.9
[22,] 840.2 412.3
[23,] 892.1 317.7
[24,] 875.3 218.8
[25,] 833.3 154.6
library(geosphere)
perimeter(m)
**Error in perimeter(m) : could not find function "perimeter"**
This will do the trick..
sample data
library( data.table )
m <- fread("x y
606.3 95.4
612.4 178.7
610.2 222.6
610.2 222.8
625.8 249.8
625.8 250.1
633.9 268.9
668.7 272.2
693.7 222.6
723.2 157.0
738.6 109.9
681.2 90.5
606.3 95.4
833.3 154.6
753.7 267.5
747.8 305.1
773.8 354.7
767.0 393.8
763.0 442.0
817.4 446.9
817.6 446.9
840.2 412.3
892.1 317.7
875.3 218.8
833.3 154.6")
code
library( grDevices )
hull <- chull( m ) #create coordinates of convex hull
coords <- m[ c( hull, hull[1]), ] # create a closed perimeter-polygon
#plot it
plot(m) #plot points
lines(coords, col="red") #plot perimeter

r why does raster extract give dim error?

I'm trying to extract values and get the mean from a raster brick, but get an error I think is related to the dimensions of the raster brick.
the data have been downloaded from NOAA
what I've done is the following:
library(raster)
ERSST <- rotate(brick('sst.mnmean.nc'))
ERSST
class : RasterBrick
dimensions : 89, 180, 16020, 1976 (nrow, ncol, ncell, nlayers)
resolution : 2, 2 (x, y)
extent : -179, 181, -89, 89 (xmin, xmax, ymin, ymax) # ignore extent, needs fixing but not relevant for the question
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0
data source : in memory
names : X1854.01.01, X1854.02.01, X1854.03.01, X1854.04.01, X1854.05.01, X1854.06.01, X1854.07.01, X1854.08.01, X1854.09.01, X1854.10.01, X1854.11.01, X1854.12.01, X1855.01.01, X1855.02.01, X1855.03.01, ...
min values : -1.8, -1.8, -1.8, -1.8, -1.8, -1.8, -1.8, -1.8, -1.8, -1.8, -1.8, -1.8, -1.8, -1.8, -1.8, ...
max values : 32.09937, 31.44189, 31.72137, 31.47466, 33.23633, 31.90788, 35.22922, 33.66898, 32.26702, 32.15502, 31.68270, 31.74512, 31.32458, 31.23049, 29.88974, ...
Date : 1854-01-01, 2018-08-01 (min, max)
Arbitrary point
xy <- data.frame(x = -49, y = 45)
and when I extract I get:
extract(ERSST, xy, buffer = 1e+05, small = TRUE, fun = mean)
Error in apply(x, 2, fun2) : dim(X) must have a positive length
what makes me think it's a dimension problem is the error I get when I try to specify the layers to use
extract(ERSST, xy, buffer = 1e+05, small = TRUE, layer = 10, nl = 10)
Error in x[, lyrs] : incorrect number of dimensions
and it seems to work fine if I average first (but this is not what I want, I need the time series at the point)
mERSST <- mean(ERSST)
extract(mERSST, xy, buffer = 1e+05, small = TRUE, fun = mean)
[1] 5.649212
Perhaps it's the Date attribute in the raster brick. Any workarounds or solutions to prevent this error?
The answer by #RobertHijmans made me realise that I always get just a single value from extract, even when the point is at the junction of several grid cells, as in the example above.
plot(MSST, xlim = c(-60, -40), ylim = c(40, 50))
points(xy)
Using:
extract(mERSST, xy, buffer = 1e+05, small = TRUE, cellnumbers = TRUE)
[[1]]
cell value
3845.000000 5.649212
I get only a single value, whereas I would expect there to be 4, no matter how small the buffer. Am I missing something in extract? So I tried with converting my point to a circle and use that to extract data
coordinates(xy) <- ~ x + y
proj4string(xy) <- '+init=epsg:4326'
xy_utm <- spTransform(xy, CRS('+init=epsg:32621'))
gbf_utm <- rgeos::gBuffer(xy_utm, width = 1e5, quadsegs = 250L)
gbf <- spTransform(gbf_utm, CRS(proj4string(xy)))
plot(ERSST[[1]], xlim = c(-60, -40), ylim = c(40, 50))
points(xy, pch = 19)
plot(gbf, add = TRUE)
extract(ERSST[[1]], gbf, small = TRUE, weights = TRUE)
this gives me:
[[1]]
value weight
[1,] 1.722664 0.25
[2,] 3.683457 0.25
[3,] 5.985203 0.25
[4,] 8.442450 0.25
in version 2.6.7 (and this seems to make sense).
but
[[1]]
value weight
[1,] 1.722664 0.001236928
[2,] 1.722664 0.003935680
[3,] 1.722664 0.005285056
[4,] 3.683457 0.005285056
[5,] 3.683457 0.003935680
[6,] 3.683457 0.001236928
[7,] 1.722664 0.002136512
[8,] 1.722664 0.008321151
[9,] 1.722664 0.011244799
[10,] 1.722664 0.011244799
[11,] 1.722664 0.011244799
[12,] 3.683457 0.011244799
[13,] 3.683457 0.011244799
[14,] 3.683457 0.011244799
[15,] 3.683457 0.008208703
[16,] 3.683457 0.001911616
[17,] 1.722664 0.003036096
[18,] 1.722664 0.010907455
[19,] 1.722664 0.011244799
[20,] 1.722664 0.011244799
[21,] 1.722664 0.011244799
[22,] 1.722664 0.011244799
[23,] 3.683457 0.011244799
[24,] 3.683457 0.011244799
[25,] 3.683457 0.011244799
[26,] 3.683457 0.011244799
[27,] 3.683457 0.010907455
[28,] 3.683457 0.003036096
[29,] 1.722664 0.000449792
[30,] 1.722664 0.010232767
[31,] 1.722664 0.011244799
[32,] 1.722664 0.011244799
[33,] 1.722664 0.011244799
[34,] 1.722664 0.011244799
[35,] 1.722664 0.011244799
[36,] 3.683457 0.011244799
[37,] 3.683457 0.011244799
[38,] 3.683457 0.011244799
[39,] 3.683457 0.011244799
[40,] 3.683457 0.011244799
[41,] 3.683457 0.010232767
[42,] 3.683457 0.000337344
[43,] 1.722664 0.003036096
[44,] 1.722664 0.011244799
[45,] 1.722664 0.011244799
[46,] 1.722664 0.011244799
[47,] 1.722664 0.011244799
[48,] 1.722664 0.011244799
[49,] 1.722664 0.011244799
[50,] 3.683457 0.011244799
[51,] 3.683457 0.011244799
[52,] 3.683457 0.011244799
[53,] 3.683457 0.011244799
[54,] 3.683457 0.011244799
[55,] 3.683457 0.011244799
[56,] 3.683457 0.002923648
[57,] 5.985203 0.002923648
[58,] 5.985203 0.011244799
[59,] 5.985203 0.011244799
[60,] 5.985203 0.011244799
[61,] 5.985203 0.011244799
[62,] 5.985203 0.011244799
[63,] 5.985203 0.011244799
[64,] 8.442450 0.011244799
[65,] 8.442450 0.011244799
[66,] 8.442450 0.011244799
[67,] 8.442450 0.011244799
[68,] 8.442450 0.011244799
[69,] 8.442450 0.011244799
[70,] 8.442450 0.002923648
[71,] 5.985203 0.000337344
[72,] 5.985203 0.010120319
[73,] 5.985203 0.011244799
[74,] 5.985203 0.011244799
[75,] 5.985203 0.011244799
[76,] 5.985203 0.011244799
[77,] 5.985203 0.011244799
[78,] 8.442450 0.011244799
[79,] 8.442450 0.011244799
[80,] 8.442450 0.011244799
[81,] 8.442450 0.011244799
[82,] 8.442450 0.011244799
[83,] 8.442450 0.010007871
[84,] 8.442450 0.000224896
[85,] 5.985203 0.002811200
[86,] 5.985203 0.010795007
[87,] 5.985203 0.011244799
[88,] 5.985203 0.011244799
[89,] 5.985203 0.011244799
[90,] 5.985203 0.011244799
[91,] 8.442450 0.011244799
[92,] 8.442450 0.011244799
[93,] 8.442450 0.011244799
[94,] 8.442450 0.011244799
[95,] 8.442450 0.010682559
[96,] 8.442450 0.002698752
[97,] 5.985203 0.001799168
[98,] 5.985203 0.007871359
[99,] 5.985203 0.011244799
[100,] 5.985203 0.011244799
[101,] 5.985203 0.011244799
[102,] 8.442450 0.011244799
[103,] 8.442450 0.011244799
[104,] 8.442450 0.011244799
[105,] 8.442450 0.007871359
[106,] 8.442450 0.001799168
[107,] 5.985203 0.001236928
[108,] 5.985203 0.003935680
[109,] 5.985203 0.005285056
[110,] 8.442450 0.005285056
[111,] 8.442450 0.003935680
[112,] 8.442450 0.001236928
in version 2.7-13, which can't be right.
To illustrate the discussion above in a simplified manner (and after fixing the extract with polygons issue), I get
library(raster)
r <- raster(xmn=-59, xmx=-39, ymn=41, ymx=49, res=2, vals=1:40)
xy <- SpatialPoints(data.frame(x = -49, y = 45), proj4string = CRS('+init=epsg:4326'))
p <- buffer(xy, width = 1e5, quadsegs = 250L)
plot(r)
plot(p, add=T)
extract(r, xy)
#26
extract(r, p)
#[[1]]
#[1] 16 26 25 15
extract(r, p, weights=T)
#[[1]]
# value weight
#[1,] 15 0.25
#[2,] 16 0.25
#[3,] 25 0.25
#[4,] 26 0.25
extract(r, xy, buffer=100000)
#[[1]]
#value
# 15
extract(r, xy, buffer=1000000)
#[[1]]
# [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
#[37] 37 38 39 40
It looks like the layer and nl arguments are not working. I am not sure why.
One workaround is to extract values first and then subset the values.
library(raster)
value <- extract(ERSST, point, buffer = 1e+05, small = TRUE)
value[[1]][10:19]
# X1854.10.01 X1854.11.01 X1854.12.01 X1855.01.01 X1855.02.01 X1855.03.01 X1855.04.01 X1855.05.01
# 7.932416 5.712043 4.428292 3.010927 2.289096 2.385752 2.528488 3.261783
# X1855.06.01 X1855.07.01
# 5.762860 7.617740

Runif generating NaN terms

I have a matrix of baseline values for different parameters, which is a 1 column x 305 row matrix, and I want to use runif to generate a matrix of random numbers (100 for each parameter value) between +- 10% of the initial parameter value. The problem is that there are NaN terms randomly interspersed in the output when there shouldn't be, and I get this warning message:
Warning message:
In runif(n = 30500, min = lower, max = upper) : NAs produced
This is my code so far:
baseline <- scan("baseline.txt")
baseline <- as.matrix(baseline)
base <- t(baseline)
upper <- 1.1 * base
lower <- 0.9 * base
matrice2 <- matrix(runif(n=30500, min = lower, max = upper), nrow=305)
(I had to take t(baseline) so that the resulting matrice2 is in the right form to be passed to another function that I'm using later). The numbers in baseline range from -0.281 to 800.
Anyone have any ideas as to why it is generating NaN? I was using this exact code before I took some time off for the holidays, and it was working then.
EDIT: this is the baseline.txt data, a 305 row x 1 column text file
baseline
[,1]
[1,] 0.400000000
[2,] 0.800000000
[3,] 0.200000000
[4,] 0.800000000
[5,] 0.600000000
[6,] 0.800000000
[7,] 0.150000000
[8,] 0.900000000
[9,] 0.500000000
[10,] 0.600000000
[11,] 0.500000000
[12,] 0.500000000
[13,] 0.600000000
[14,] 0.500000000
[15,] 0.500000000
[16,] 0.600000000
[17,] 0.200000000
[18,] 0.700000000
[19,] 0.800000000
[20,] 0.300000000
[21,] 0.700000000
[22,] 0.800000000
[23,] 0.300000000
[24,] 0.700000000
[25,] 0.800000000
[26,] 0.200000000
[27,] 0.002000000
[28,] 0.400000000
[29,] 0.001000000
[30,] 0.001300000
[31,] 0.800000000
[32,] 0.526000000
[33,] 0.526000000
[34,] 0.714000000
[35,] 0.306000000
[36,] 0.306000000
[37,] 0.711000000
[38,] 0.711000000
[39,] 0.847000000
[40,] 0.300000000
[41,] 0.300000000
[42,] 0.300000000
[43,] 0.300000000
[44,] 0.300000000
[45,] 0.300000000
[46,] 0.050000000
[47,] 0.400000000
[48,] 0.450000000
[49,] 0.300000000
[50,] 0.400000000
[51,] 0.700000000
[52,] 0.400000000
[53,] 0.600000000
[54,] 0.306000000
[55,] 0.885000000
[56,] 0.008610000
[57,] 0.008610000
[58,] 0.008610000
[59,] 0.008610000
[60,] 0.016500000
[61,] 6.450000000
[62,] 6.450000000
[63,] 6.450000000
[64,] 6.450000000
[65,] 0.014650000
[66,] 0.014650000
[67,] 0.014650000
[68,] 0.014650000
[69,] 0.200000000
[70,] 0.400000000
[71,] 0.400000000
[72,] 3.000000000
[73,] 3.000000000
[74,] 3.000000000
[75,] 3.000000000
[76,] 3.000000000
[77,] 3.000000000
[78,] 3.000000000
[79,] 3.000000000
[80,] 3.000000000
[81,] 0.338000000
[82,] 0.013200000
[83,] 0.001350000
[84,] 0.001350000
[85,] 0.001350000
[86,] 0.338000000
[87,] 34.694000000
[88,] 0.002710000
[89,] 0.000420000
[90,] 0.015900000
[91,] 0.037000000
[92,] 0.024500000
[93,] 0.075000000
[94,] 0.045000000
[95,] 0.024800000
[96,] 0.015900000
[97,] 0.045000000
[98,] 0.045000000
[99,] 2.800000000
[100,] 0.001590000
[101,] 0.004500000
[102,] 0.001000000
[103,] 0.020000000
[104,] 0.004800000
[105,] 7.500000000
[106,] 34.694000000
[107,] 0.338000000
[108,] 0.028900000
[109,] 0.022400000
[110,] 0.022400000
[111,] 0.040000000
[112,] 0.008000000
[113,] 0.338000000
[114,] 0.026400000
[115,] 0.046080000
[116,] 0.057600000
[117,] 0.700000000
[118,] 0.400000000
[119,] 0.200000000
[120,] 0.700000000
[121,] 0.300000000
[122,] 0.750000000
[123,] 4.000000000
[124,] 0.500000000
[125,] 8.000000000
[126,] 800.000000000
[127,] 3.000000000
[128,] 3.000000000
[129,] 6.480000000
[130,] 1.150000000
[131,] 0.700000000
[132,] 0.100000000
[133,] 1.793000000
[134,] 1.291000000
[135,] 1.793000000
[136,] 1.291000000
[137,] 1.793000000
[138,] 1.291000000
[139,] 5.944000000
[140,] 4.877000000
[141,] 0.000210000
[142,] 0.006700000
[143,] 2.000000000
[144,] 0.080000000
[145,] 0.110000000
[146,] 0.008500000
[147,] 120.000000000
[148,] 60.000000000
[149,] 3.200000000
[150,] 0.800000000
[151,] 1.300000000
[152,] 1.300000000
[153,] 1.300000000
[154,] 2.690000000
[155,] 0.024500000
[156,] 7.850000000
[157,] 1.500000000
[158,] 10.000000000
[159,] 10.000000000
[160,] 4.300000000
[161,] 0.050000000
[162,] 0.400000000
[163,] 7.860000000
[164,] 3.000000000
[165,] 0.052800000
[166,] 0.052800000
[167,] 15.439000000
[168,] 7.860000000
[169,] 15.439000000
[170,] 0.200000000
[171,] 0.053000000
[172,] 0.022000000
[173,] 0.164600000
[174,] 0.164600000
[175,] 0.164600000
[176,] 0.164600000
[177,] 4.100000000
[178,] 15.439000000
[179,] 7.860000000
[180,] 0.160000000
[181,] 0.040000000
[182,] 0.014400000
[183,] 7.860000000
[184,] 6.000000000
[185,] 0.300000000
[186,] 0.300000000
[187,] 33.500000000
[188,] 0.020000000
[189,] 1.277000000
[190,] 0.981700000
[191,] 1.000000000
[192,] 1.000000000
[193,] 1.000000000
[194,] 0.016666667
[195,] 0.271000000
[196,] 0.504000000
[197,] 0.504000000
[198,] 0.504000000
[199,] 0.713000000
[200,] 0.742000000
[201,] 0.713000000
[202,] 0.981700000
[203,] 0.058800000
[204,] 1.257000000
[205,] 1.257000000
[206,] 2.723000000
[207,] 3.969000000
[208,] 2.462000000
[209,] 2.492000000
[210,] 3.000000000
[211,] 0.815000000
[212,] 1.191000000
[213,] 1.191000000
[214,] 1.191000000
[215,] 2.241000000
[216,] 3.969000000
[217,] 2.241000000
[218,] 3.969000000
[219,] 152.000000000
[220,] 0.085000000
[221,] 0.493900000
[222,] 0.231000000
[223,] 0.246000000
[224,] 0.500000000
[225,] 0.011363636
[226,] 0.133000000
[227,] 0.133000000
[228,] 0.133000000
[229,] 0.198000000
[230,] 0.493900000
[231,] 0.198000000
[232,] 0.493900000
[233,] 1.447000000
[234,] 1.970000000
[235,] 0.995000000
[236,] 0.995000000
[237,] 1.000000000
[238,] 0.271000000
[239,] 0.623000000
[240,] 0.623000000
[241,] 0.623000000
[242,] 1.128000000
[243,] 1.970000000
[244,] 1.128000000
[245,] 1.970000000
[246,] 2.256000000
[247,] 2.951000000
[248,] 3.000000000
[249,] 3.000000000
[250,] 3.000000000
[251,] -0.281000000
[252,] 1.065000000
[253,] 1.065000000
[254,] 1.065000000
[255,] 0.949000000
[256,] 2.951000000
[257,] 0.949000000
[258,] 2.951000000
[259,] 0.000840000
[260,] 0.000120000
[261,] 0.006200000
[262,] 0.006200000
[263,] 0.006200000
[264,] 0.085000000
[265,] 0.030500000
[266,] 0.459000000
[267,] 0.030500000
[268,] 0.459000000
[269,] 0.030500000
[270,] 0.011100000
[271,] 0.001481000
[272,] 0.000210000
[273,] 0.000210000
[274,] 0.000210000
[275,] 0.000210000
[276,] 142.050000000
[277,] 0.468000000
[278,] 0.355000000
[279,] 0.335000000
[280,] 0.013513514
[281,] 0.729000000
[282,] 0.154000000
[283,] 0.154000000
[284,] 0.154000000
[285,] 0.396000000
[286,] 0.396000000
[287,] 0.009100000
[288,] 0.006700000
[289,] 0.006700000
[290,] 0.006700000
[291,] 0.006700000
[292,] 112.500000000
[293,] 112.500000000
[294,] 112.500000000
[295,] 0.009803922
[296,] 0.237000000
[297,] 0.237000000
[298,] 0.237000000
[299,] 0.017900000
[300,] 0.017900000
[301,] 0.000120000
[302,] 0.000120000
[303,] 0.000120000
[304,] 0.006200000
[305,] 0.006200000
As pointed out in comments, in runif the min must be less than max. Your method of calculating min and max move the max farther from 0 and the mins closer to 0 which only works when the numbers are positive.
As suggested in comments, there are several ways you could do this, for example pmin and pmax:
upper = pmax(0.9 * base, 1.1 * base)
lower = pmin(0.9 * base, 1.1 * base)
another way, probably more efficient, would be:
offset = 0.1 * abs(base)
upper = base + offset
lower = base - offset

Principal component analysis (princomp, principal, etc.) on a 3D array

I have used PCA on 2D arrays before, and I use the first PC score vector that best best describes the variance of all the other columns in analyses. Below is a R example that shows the Comp.1 vector that would best describe the variance of the 2D array of interest.
data <- array(data=sample(12), c(4,3))
data
[,1] [,2] [,3]
[1,] 11 2 12
[2,] 4 3 10
[3,] 8 7 1
[4,] 6 9 5
output=princomp(data)
output$scores
Comp.1 Comp.2 Comp.3
[1,] 6.422813 2.865390 0.4025040
[2,] 3.251842 -3.617633 -0.9814571
[3,] -5.856500 1.848419 -1.3819379
[4,] -3.818155 -1.096176 1.9608909
My question is how can I do this same procedure on a 3D array? For example, if I have an array that the size is 4 x 5 x 3 how could I get the 4 x 5 2D array that is equivalent to the Comp.1 vector found above?
I have provided an R example below with code and outputs. When I look at the scores it only outputs one component (not 3 as expected), and the length is 60. Does that mean that the first 20 elements correspond to the first PC, the next 20 to the 2nd PC, and the last 20 to the 3rd PC? If so how does princomp arrange the entries, so I can get back to the original 4 x 5 2D array using the first 20 elements (1st PC)? Thank you for your assistance.
data=array(data=sample(48), c(4,5,3))
data
, , 1
[,1] [,2] [,3] [,4] [,5]
[1,] 47 21 45 41 34
[2,] 1 16 32 31 37
[3,] 39 8 35 10 6
[4,] 48 14 25 3 11
, , 2
[,1] [,2] [,3] [,4] [,5]
[1,] 12 43 15 36 23
[2,] 17 4 7 26 46
[3,] 2 13 33 20 40
[4,] 18 19 28 44 38
, , 3
[,1] [,2] [,3] [,4] [,5]
[1,] 42 24 47 21 45
[2,] 5 22 1 16 32
[3,] 30 29 39 8 35
[4,] 27 9 48 14 25
output=princomp(data)
output$scores
Comp.1
[1,] 21.8833333
[2,] -24.1166667
[3,] 13.8833333
[4,] 22.8833333
[5,] -4.1166667
[6,] -9.1166667
[7,] -17.1166667
[8,] -11.1166667
[9,] 19.8833333
[10,] 6.8833333
[11,] 9.8833333
[12,] -0.1166667
[13,] 15.8833333
[14,] 5.8833333
[15,] -15.1166667
[16,] -22.1166667
[17,] 8.8833333
[18,] 11.8833333
[19,] -19.1166667
[20,] -14.1166667
[21,] -13.1166667
[22,] -8.1166667
[23,] -23.1166667
[24,] -7.1166667
[25,] 17.8833333
[26,] -21.1166667
[27,] -12.1166667
[28,] -6.1166667
[29,] -10.1166667
[30,] -18.1166667
[31,] 7.8833333
[32,] 2.8833333
[33,] 10.8833333
[34,] 0.8833333
[35,] -5.1166667
[36,] 18.8833333
[37,] -2.1166667
[38,] 20.8833333
[39,] 14.8833333
[40,] 12.8833333
[41,] 16.8833333
[42,] -20.1166667
[43,] 4.8833333
[44,] 1.8833333
[45,] -1.1166667
[46,] -3.1166667
[47,] 3.8833333
[48,] -16.1166667
[49,] 21.8833333
[50,] -24.1166667
[51,] 13.8833333
[52,] 22.8833333
[53,] -4.1166667
[54,] -9.1166667
[55,] -17.1166667
[56,] -11.1166667
[57,] 19.8833333
[58,] 6.8833333
[59,] 9.8833333
[60,] -0.1166667

How to retrieve a slot coords of each spatialPpolygon object

I have a spatialPolygons object in R; but I can not to retrieve the "coords" for each spatialPolygon object
Here's my R session:
[[1]]
An object of class "Polygon"
Slot "labpt":
[1] 5.7828 20.5764
Slot "area":
[1] 0.16
Slot "hole":
[1] FALSE
Slot "ringDir":
[1] 1
Slot "coords":
x y
[1,] 5.6828 20.7764
[2,] 5.7828 20.7764
[3,] 5.8828 20.7764
[4,] 5.9828 20.7764
[5,] 5.9828 20.3764
[6,] 5.8828 20.3764
[7,] 5.7828 20.3764
[8,] 5.6828 20.3764
[9,] 5.5828 20.3764
[10,] 5.5828 20.7764
[11,] 5.6828 20.7764
[[2]]
An object of class "Polygon"
Slot "labpt":
[1] 3.715865 35.160271
Slot "area":
[1] 39.68
Slot "hole":
[1] FALSE
Slot "ringDir":
[1] 1
Slot "coords":
x y
[1,] -0.7172 35.1764
[2,] -0.8172 35.1764
[3,] -0.9172 35.1764
[4,] -1.0172 35.1764
[5,] -1.1172 35.1764
[6,] -1.1172 35.5764
[7,] -1.2172 35.5764
[8,] -1.2172 35.9764
[9,] -1.2172 36.3764
[10,] -1.3172 36.3764
[11,] -1.3172 36.7764
[12,] -1.4172 36.7764
[13,] -1.5172 36.7764
[14,] -1.6172 36.7764
[15,] -1.6172 37.1764
[16,] -1.5172 37.1764
[17,] -1.4172 37.1764
[18,] -1.3172 37.1764
[19,] -1.2172 37.1764
[20,] -1.1172 37.1764
[21,] -1.0172 37.1764
[22,] -0.9172 37.1764
[23,] -0.8172 37.1764
[24,] -0.7172 37.1764
[25,] -0.6172 37.1764
[26,] -0.5172 37.1764
[27,] -0.4172 37.1764
[28,] -0.3172 37.1764
[29,] -0.2172 37.1764
[30,] -0.1172 37.1764
[31,] -0.0172 37.1764
[32,] 0.0828 37.1764
[33,] 0.1828 37.1764
[34,] 0.2828 37.1764
[35,] 0.3828 37.1764
[36,] 0.4828 37.1764
[37,] 0.5828 37.1764
[38,] 0.6828 37.1764
[39,] 0.7828 37.1764
[40,] 0.8828 37.1764
[41,] 0.9828 37.1764
[42,] 1.0828 37.1764
[43,] 1.1828 37.1764
[44,] 1.2828 37.1764
[45,] 1.3828 37.1764
[46,] 1.4828 37.1764
[47,] 1.5828 37.1764
[48,] 1.6828 37.1764
[49,] 1.7828 37.1764
[50,] 1.8828 37.1764
[51,] 1.9828 37.1764
[52,] 2.0828 37.1764
[53,] 2.1828 37.1764
[54,] 2.2828 37.1764
[55,] 2.3828 37.1764
[56,] 2.4828 37.1764
[57,] 2.5828 37.1764
[58,] 2.6828 37.1764
[59,] 2.7828 37.1764
[60,] 2.8828 37.1764
[61,] 2.9828 37.1764
[62,] 3.0828 37.1764
[63,] 3.1828 37.1764
[64,] 3.2828 37.1764
[65,] 3.3828 37.1764
[66,] 3.4828 37.1764
[67,] 3.5828 37.1764
[68,] 3.6828 37.1764
[69,] 3.7828 37.1764
[70,] 3.8828 37.1764
[71,] 3.9828 37.1764
[72,] 4.0828 37.1764
[73,] 4.1828 37.1764
[74,] 4.2828 37.1764
[75,] 4.3828 37.1764
[76,] 4.4828 37.1764
[77,] 4.5828 37.1764
[78,] 4.6828 37.1764
[79,] 4.7828 37.1764
[80,] 4.8828 37.1764
[81,] 4.9828 37.1764
[82,] 5.0828 37.1764
[83,] 5.1828 37.1764
[84,] 5.2828 37.1764
[85,] 5.3828 37.1764
[86,] 5.4828 37.1764
[87,] 5.5828 37.1764
[88,] 5.6828 37.1764
[89,] 5.7828 37.1764
[90,] 5.8828 37.1764
[91,] 5.9828 37.1764
[92,] 6.0828 37.1764
[93,] 6.1828 37.1764
[94,] 6.2828 37.1764
[95,] 6.3828 37.1764
[96,] 6.4828 37.1764
[97,] 6.5828 37.1764
[98,] 6.6828 37.1764
[99,] 6.7828 37.1764
[100,] 6.8828 37.1764
[101,] 6.9828 37.1764
[102,] 7.0828 37.1764
[103,] 7.1828 37.1764
[104,] 7.2828 37.1764
[105,] 7.3828 37.1764
[106,] 7.4828 37.1764
[107,] 7.5828 37.1764
[108,] 7.6828 37.1764
[109,] 7.7828 37.1764
[110,] 7.8828 37.1764
[111,] 7.9828 37.1764
[112,] 8.0828 37.1764
[113,] 8.1828 37.1764
[114,] 8.2828 37.1764
[115,] 8.3828 37.1764
[116,] 8.4828 37.1764
[117,] 8.5828 37.1764
[118,] 8.6828 37.1764
[119,] 8.7828 37.1764
[120,] 8.8828 37.1764
[121,] 8.9828 37.1764
[122,] 9.0828 37.1764
[123,] 9.1828 37.1764
[124,] 9.2828 37.1764
[125,] 9.3828 37.1764
[126,] 9.4828 37.1764
[127,] 9.5828 37.1764
[128,] 9.6828 37.1764
[129,] 9.7828 37.1764
[130,] 9.8828 37.1764
[131,] 9.9828 37.1764
[132,] 10.0828 37.1764
[133,] 10.1828 37.1764
[134,] 10.2828 37.1764
[135,] 10.3828 37.1764
[136,] 10.4828 37.1764
[137,] 10.5828 37.1764
[138,] 10.6828 37.1764
[139,] 10.7828 37.1764
[140,] 10.8828 37.1764
[141,] 10.9828 37.1764
[142,] 11.0828 37.1764
[143,] 11.1828 37.1764
[144,] 11.2828 37.1764
[145,] 11.3828 37.1764
[146,] 11.4828 37.1764
[147,] 11.5828 37.1764
[148,] 11.6828 37.1764
[149,] 11.7828 37.1764
[150,] 11.8828 37.1764
[151,] 11.9828 37.1764
[152,] 11.9828 36.7764
[153,] 11.9828 36.3764
[154,] 11.9828 35.9764
[155,] 11.9828 35.5764
[156,] 11.9828 35.1764
[157,] 11.8828 35.1764
[158,] 11.7828 35.1764
[159,] 11.6828 35.1764
[160,] 11.5828 35.1764
[161,] 11.4828 35.1764
[162,] 11.3828 35.1764
[163,] 11.2828 35.1764
[164,] 11.1828 35.1764
[165,] 11.0828 35.1764
[166,] 10.9828 35.1764
[167,] 10.8828 35.1764
[168,] 10.7828 35.1764
[169,] 10.6828 35.1764
[170,] 10.5828 35.1764
[171,] 10.4828 35.1764
[172,] 10.3828 35.1764
[173,] 10.2828 35.1764
[174,] 10.1828 35.1764
[175,] 10.0828 35.1764
[176,] 9.9828 35.1764
[177,] 9.8828 35.1764
[178,] 9.7828 35.1764
[179,] 9.6828 35.1764
[180,] 9.5828 35.1764
[181,] 9.4828 35.1764
[182,] 9.3828 35.1764
[183,] 9.2828 35.1764
[184,] 9.2828 35.5764
[185,] 9.1828 35.5764
[186,] 9.0828 35.5764
[187,] 8.9828 35.5764
[188,] 8.8828 35.5764
[189,] 8.7828 35.5764
[190,] 8.6828 35.5764
[191,] 8.6828 35.9764
[192,] 8.5828 35.9764
[193,] 8.5828 36.3764
[194,] 8.4828 36.3764
[195,] 8.3828 36.3764
[196,] 8.2828 36.3764
[197,] 8.1828 36.3764
[198,] 8.0828 36.3764
[199,] 8.0828 36.7764
[200,] 7.9828 36.7764
[201,] 7.8828 36.7764
[202,] 7.8828 36.3764
[203,] 7.8828 35.9764
[204,] 7.7828 35.9764
[205,] 7.7828 35.5764
[206,] 7.6828 35.5764
[207,] 7.5828 35.5764
[208,] 7.4828 35.5764
[209,] 7.4828 35.1764
[210,] 7.3828 35.1764
[211,] 7.2828 35.1764
[212,] 7.1828 35.1764
[213,] 7.0828 35.1764
[214,] 6.9828 35.1764
[215,] 6.8828 35.1764
[216,] 6.7828 35.1764
[217,] 6.7828 35.5764
[218,] 6.8828 35.5764
[219,] 6.8828 35.9764
[220,] 6.7828 35.9764
[221,] 6.7828 36.3764
[222,] 6.6828 36.3764
[223,] 6.5828 36.3764
[224,] 6.4828 36.3764
[225,] 6.4828 36.7764
[226,] 6.3828 36.7764
[227,] 6.2828 36.7764
[228,] 6.1828 36.7764
[229,] 6.0828 36.7764
[230,] 5.9828 36.7764
[231,] 5.9828 36.3764
[232,] 5.9828 35.9764
[233,] 5.8828 35.9764
[234,] 5.7828 35.9764
[235,] 5.7828 35.5764
[236,] 5.7828 35.1764
[237,] 5.6828 35.1764
[238,] 5.5828 35.1764
[239,] 5.4828 35.1764
[240,] 5.3828 35.1764
[241,] 5.2828 35.1764
[242,] 5.1828 35.1764
[243,] 5.0828 35.1764
[244,] 4.9828 35.1764
[245,] 4.9828 34.7764
[246,] 4.8828 34.7764
[247,] 4.7828 34.7764
[248,] 4.6828 34.7764
[249,] 4.6828 34.3764
[250,] 4.5828 34.3764
[251,] 4.4828 34.3764
[252,] 4.4828 33.9764
[253,] 4.3828 33.9764
[254,] 4.2828 33.9764
[255,] 4.2828 33.5764
[256,] 4.1828 33.5764
[257,] 4.0828 33.5764
[258,] 3.9828 33.5764
[259,] 3.9828 33.1764
[260,] 3.8828 33.1764
[261,] 3.7828 33.1764
[262,] 3.6828 33.1764
[263,] 3.5828 33.1764
[264,] 3.4828 33.1764
[265,] 3.3828 33.1764
[266,] 3.3828 32.7764
[267,] 3.2828 32.7764
[268,] 3.1828 32.7764
[269,] 3.0828 32.7764
[270,] 3.0828 32.3764
[271,] 2.9828 32.3764
[272,] 2.8828 32.3764
[273,] 2.7828 32.3764
[274,] 2.7828 31.9764
[275,] 2.6828 31.9764
[276,] 2.5828 31.9764
[277,] 2.4828 31.9764
[278,] 2.3828 31.9764
[279,] 2.3828 31.5764
[280,] 2.2828 31.5764
[281,] 2.1828 31.5764
[282,] 2.0828 31.5764
[283,] 1.9828 31.5764
[284,] 1.8828 31.5764
[285,] 1.7828 31.5764
[286,] 1.6828 31.5764
[287,] 1.5828 31.5764
[288,] 1.4828 31.5764
[289,] 1.3828 31.5764
[290,] 1.2828 31.5764
[291,] 1.1828 31.5764
[292,] 1.0828 31.5764
[293,] 0.9828 31.5764
[294,] 0.8828 31.5764
[295,] 0.7828 31.5764
[296,] 0.6828 31.5764
[297,] 0.5828 31.5764
[298,] 0.4828 31.5764
[299,] 0.3828 31.5764
[300,] 0.2828 31.5764
[301,] 0.1828 31.5764
[302,] 0.0828 31.5764
[303,] -0.0172 31.5764
[304,] -0.0172 31.9764
[305,] -0.1172 31.9764
[306,] -0.2172 31.9764
[307,] -0.3172 31.9764
[308,] -0.4172 31.9764
[309,] -0.5172 31.9764
[310,] -0.6172 31.9764
[311,] -0.7172 31.9764
[312,] -0.7172 32.3764
[313,] -0.8172 32.3764
[314,] -0.9172 32.3764
[315,] -1.0172 32.3764
[316,] -1.0172 32.7764
[317,] -1.1172 32.7764
[318,] -1.1172 33.1764
[319,] -1.0172 33.1764
[320,] -1.0172 33.5764
[321,] -0.9172 33.5764
[322,] -0.8172 33.5764
[323,] -0.8172 33.9764
[324,] -0.7172 33.9764
[325,] -0.6172 33.9764
[326,] -0.5172 33.9764
[327,] -0.4172 33.9764
[328,] -0.3172 33.9764
[329,] -0.3172 34.3764
[330,] -0.2172 34.3764
[331,] -0.2172 34.7764
[332,] -0.3172 34.7764
[333,] -0.4172 34.7764
[334,] -0.5172 34.7764
[335,] -0.5172 35.1764
[336,] -0.6172 35.1764
[337,] -0.7172 35.1764
[[3]]
An object of class "Polygon"
Slot "labpt":
[1] 1.199467 36.200642
Slot "area":
[1] 1.32
Slot "hole":
[1] TRUE
Slot "ringDir":
[1] -1
Slot "coords":
x y
[1,] 0.7828 35.5764
[2,] 0.8828 35.5764
[3,] 0.9828 35.5764
[4,] 1.0828 35.5764
[5,] 1.1828 35.5764
[6,] 1.2828 35.5764
[7,] 1.3828 35.5764
[8,] 1.4828 35.5764
[9,] 1.5828 35.5764
[10,] 1.6828 35.5764
[11,] 1.6828 35.9764
[12,] 1.7828 35.9764
[13,] 1.8828 35.9764
[14,] 1.8828 36.3764
[15,] 1.7828 36.3764
[16,] 1.6828 36.3764
[17,] 1.6828 36.7764
[18,] 1.5828 36.7764
[19,] 1.4828 36.7764
[20,] 1.3828 36.7764
[21,] 1.2828 36.7764
[22,] 1.1828 36.7764
[23,] 1.0828 36.7764
[24,] 0.9828 36.7764
[25,] 0.8828 36.7764
[26,] 0.7828 36.7764
[27,] 0.6828 36.7764
[28,] 0.5828 36.7764
[29,] 0.5828 36.3764
[30,] 0.5828 35.9764
[31,] 0.6828 35.9764
[32,] 0.7828 35.9764
[33,] 0.7828 35.5764
[[4]]
An object of class "Polygon"
Slot "labpt":
[1] 2.8828 33.6164
Slot "area":
[1] 0.4
Slot "hole":
[1] TRUE
Slot "ringDir":
[1] -1
Slot "coords":
x y
[1,] 2.5828 33.9764
[2,] 2.5828 33.5764
[3,] 2.6828 33.5764
[4,] 2.6828 33.1764
[5,] 2.7828 33.1764
[6,] 2.8828 33.1764
[7,] 2.9828 33.1764
[8,] 3.0828 33.1764
[9,] 3.0828 33.5764
[10,] 3.1828 33.5764
[11,] 3.1828 33.9764
[12,] 3.0828 33.9764
[13,] 2.9828 33.9764
[14,] 2.8828 33.9764
[15,] 2.7828 33.9764
[16,] 2.6828 33.9764
[17,] 2.5828 33.9764
so I want to extract the
Slot "coords" from [[1]] in a dataframe ,Slot "coords" from[[2]] in another dataframe so on...
can someone help me please?
edit :
for example, as extracting coordinates???
[1,] 5.6828 20.7764
[2,] 5.7828 20.7764
[3,] 5.8828 20.7764
[4,] 5.9828 20.7764
[5,] 5.9828 20.3764
[6,] 5.8828 20.3764
[7,] 5.7828 20.3764
[8,] 5.6828 20.3764
[9,] 5.5828 20.3764
[10,] 5.5828 20.7764
[11,] 5.6828 20.7764
A SpatialPolygons object is a list of polygons, each of which is a list of Polygons (yes, the only difference is in the capitalization...). The latter has the coordinates.
Here is an example of provinces in France, downloaded from GADM:
library(raster)
FR <- getData('GADM',country='FRA',level=1)
length(FR#polygons)
# [1] 22
sapply(FR#polygons,function(p)length(p#Polygons))
# [1] 1 1 1 2 1 28 2 2 1 1 2 16 245 10 11 3 1 2 1 8 28 23
So FR is a spatialPolygons object representing French provinces. There are 22 polygons representing the provinces, and each of these has one or more Polygons containing the physical coordinates (e.g., lat and long) of the boundary. There can be multiple Polygons because the geographies can be dis-contiguous (e.g. islands, etc.). In fact the land-locked provinces generally have just one Polygon, whereas the coastal provinces have many.
So do you want 22 data frames, one for each province, or you so want a dataframe for each geography?
If you know that your spatiaPolygons object has only one Polygon for each polygon, then you could do it this way:
result <- lapply(FR#polygons,function(p) data.frame(p#Polygons[[1]]#coords))
In this case I'm just grabbing the coordinates of the first Polygon in each province.
Wouldn't this just be?
name_of_list_of_objects[[1]]#coords
Usually there is an extraction function for an S4 object that is considered safer and more correct, so this might work:
coordinates( name_of_list_object[[1]] )
So for multiple items, just:
lapply(name_of_list_of_objects, coordinates)

Resources