Search code examples
rlistmapply

List [CTD files] - object of type 'S4' is not subsettable [R]


I would like to extract casts ("ctd_files") from unique ctd file in order to applied formulas from the package oce in all the casts with lapply function. Nevertheless, I obtain this error:

Error in files[ifile] : object of type 'S4' is not subsettable

For that I use this dataframe (sample):

df <- 
structure(list(ctd_file = c("2018-08-02T162038 SBE0251090", "2018-08-02T162038 SBE0251090", 
"2018-08-02T162038 SBE0251090", "2018-08-02T162038 SBE0251090", 
"2018-08-02T162038 SBE0251090", "2018-08-02T162038 SBE0251090", 
"2018-08-02T162038 SBE0251090", "2018-08-02T162038 SBE0251090", 
"2018-08-02T162038 SBE0251090", "2018-08-02T162038 SBE0251090", 
"2018-08-02T162038 SBE0251090", "2018-08-02T162038 SBE0251090", 
"2018-08-02T162038 SBE0251090", "2018-08-02T162038 SBE0251090", 
"2018-08-02T162038 SBE0251090", "2018-08-02T162038 SBE0251090", 
"2018-08-02T162038 SBE0251090", "2018-08-02T162038 SBE0251090", 
"2018-08-02T162038 SBE0251090", "2018-08-02T162038 SBE0251090", 
"2018-08-03T090018 SBE0251090", "2018-08-03T090018 SBE0251090", 
"2018-08-03T090018 SBE0251090", "2018-08-03T090018 SBE0251090", 
"2018-08-03T090018 SBE0251090", "2018-08-03T090018 SBE0251090", 
"2018-08-03T090018 SBE0251090", "2018-08-03T090018 SBE0251090", 
"2018-08-03T090018 SBE0251090", "2018-08-03T090018 SBE0251090", 
"2018-08-03T090018 SBE0251090", "2018-08-03T090018 SBE0251090", 
"2018-08-03T090018 SBE0251090", "2018-08-03T090018 SBE0251090", 
"2018-08-03T090018 SBE0251090", "2018-08-03T090018 SBE0251090", 
"2018-08-03T090018 SBE0251090", "2018-08-03T090018 SBE0251090", 
"2018-08-03T090018 SBE0251090", "2018-08-03T090018 SBE0251090", 
"2018-08-03T183128 SBE0251090", "2018-08-03T183128 SBE0251090", 
"2018-08-03T183128 SBE0251090", "2018-08-03T183128 SBE0251090", 
"2018-08-03T183128 SBE0251090", "2018-08-03T183128 SBE0251090", 
"2018-08-03T183128 SBE0251090", "2018-08-03T183128 SBE0251090", 
"2018-08-03T183128 SBE0251090", "2018-08-03T183128 SBE0251090", 
"2018-08-03T183128 SBE0251090", "2018-08-03T183128 SBE0251090", 
"2018-08-03T183128 SBE0251090", "2018-08-03T183128 SBE0251090", 
"2018-08-03T183128 SBE0251090", "2018-08-03T183128 SBE0251090", 
"2018-08-03T183128 SBE0251090", "2018-08-03T183128 SBE0251090", 
"2018-08-03T183128 SBE0251090", "2018-08-03T183128 SBE0251090", 
"2018-08-05T092503 SBE0251090", "2018-08-05T092503 SBE0251090", 
"2018-08-05T092503 SBE0251090", "2018-08-05T092503 SBE0251090", 
"2018-08-05T092503 SBE0251090", "2018-08-05T092503 SBE0251090", 
"2018-08-05T092503 SBE0251090", "2018-08-05T092503 SBE0251090", 
"2018-08-05T092503 SBE0251090", "2018-08-05T092503 SBE0251090", 
"2018-08-05T092503 SBE0251090", "2018-08-05T092503 SBE0251090", 
"2018-08-05T092503 SBE0251090", "2018-08-05T092503 SBE0251090", 
"2018-08-05T092503 SBE0251090", "2018-08-05T092503 SBE0251090", 
"2018-08-05T092503 SBE0251090", "2018-08-05T092503 SBE0251090", 
"2018-08-05T092503 SBE0251090", "2018-08-05T092503 SBE0251090", 
"2018-08-05T190218 SBE0251090", "2018-08-05T190218 SBE0251090", 
"2018-08-05T190218 SBE0251090", "2018-08-05T190218 SBE0251090", 
"2018-08-05T190218 SBE0251090", "2018-08-05T190218 SBE0251090", 
"2018-08-05T190218 SBE0251090", "2018-08-05T190218 SBE0251090", 
"2018-08-05T190218 SBE0251090", "2018-08-05T190218 SBE0251090", 
"2018-08-05T190218 SBE0251090", "2018-08-05T190218 SBE0251090", 
"2018-08-05T190218 SBE0251090", "2018-08-05T190218 SBE0251090", 
"2018-08-05T190218 SBE0251090", "2018-08-05T190218 SBE0251090", 
"2018-08-05T190218 SBE0251090", "2018-08-05T190218 SBE0251090", 
"2018-08-05T190218 SBE0251090", "2018-08-05T190218 SBE0251090"
), LAT = c(69.27166667, 69.27166667, 69.27166667, 69.27166667, 
69.27166667, 69.27166667, 69.27166667, 69.27166667, 69.27166667, 
69.27166667, 69.27166667, 69.27166667, 69.27166667, 69.27166667, 
69.27166667, 69.27166667, 69.27166667, 69.27166667, 69.27166667, 
69.27166667, 69.23883333, 69.23883333, 69.23883333, 69.23883333, 
69.23883333, 69.23883333, 69.23883333, 69.23883333, 69.23883333, 
69.23883333, 69.23883333, 69.23883333, 69.23883333, 69.23883333, 
69.23883333, 69.23883333, 69.23883333, 69.23883333, 69.23883333, 
69.23883333, 69.11011667, 69.11011667, 69.11011667, 69.11011667, 
69.11011667, 69.11011667, 69.11011667, 69.11011667, 69.11011667, 
69.11011667, 69.11011667, 69.11011667, 69.11011667, 69.11011667, 
69.11011667, 69.11011667, 69.11011667, 69.11011667, 69.11011667, 
69.11011667, 69.01643333, 69.01643333, 69.01643333, 69.01643333, 
69.01643333, 69.01643333, 69.01643333, 69.01643333, 69.01643333, 
69.01643333, 69.01643333, 69.01643333, 69.01643333, 69.01643333, 
69.01643333, 69.01643333, 69.01643333, 69.01643333, 69.01643333, 
69.01643333, 68.64991667, 68.64991667, 68.64991667, 68.64991667, 
68.64991667, 68.64991667, 68.64991667, 68.64991667, 68.64991667, 
68.64991667, 68.64991667, 68.64991667, 68.64991667, 68.64991667, 
68.64991667, 68.64991667, 68.64991667, 68.64991667, 68.64991667, 
68.64991667), LONG = c(-25.15166667, -25.15166667, -25.15166667, 
-25.15166667, -25.15166667, -25.15166667, -25.15166667, -25.15166667, 
-25.15166667, -25.15166667, -25.15166667, -25.15166667, -25.15166667, 
-25.15166667, -25.15166667, -25.15166667, -25.15166667, -25.15166667, 
-25.15166667, -25.15166667, -24.9915, -24.9915, -24.9915, -24.9915, 
-24.9915, -24.9915, -24.9915, -24.9915, -24.9915, -24.9915, -24.9915, 
-24.9915, -24.9915, -24.9915, -24.9915, -24.9915, -24.9915, -24.9915, 
-24.9915, -24.9915, -24.38488333, -24.38488333, -24.38488333, 
-24.38488333, -24.38488333, -24.38488333, -24.38488333, -24.38488333, 
-24.38488333, -24.38488333, -24.38488333, -24.38488333, -24.38488333, 
-24.38488333, -24.38488333, -24.38488333, -24.38488333, -24.38488333, 
-24.38488333, -24.38488333, -23.99908333, -23.99908333, -23.99908333, 
-23.99908333, -23.99908333, -23.99908333, -23.99908333, -23.99908333, 
-23.99908333, -23.99908333, -23.99908333, -23.99908333, -23.99908333, 
-23.99908333, -23.99908333, -23.99908333, -23.99908333, -23.99908333, 
-23.99908333, -23.99908333, -23.07351667, -23.07351667, -23.07351667, 
-23.07351667, -23.07351667, -23.07351667, -23.07351667, -23.07351667, 
-23.07351667, -23.07351667, -23.07351667, -23.07351667, -23.07351667, 
-23.07351667, -23.07351667, -23.07351667, -23.07351667, -23.07351667, 
-23.07351667, -23.07351667), timeS = c(458.472, 304.014, 607.791, 
340.571, 506.352, 240.956, 426.707, 316.396, 437.559, 533.356, 
377.314, 535.885, 607.141, 228.871, 553.004, 408.34, 265.546, 
509.16, 596.01, 488.236, 803.31, 622.76, 686.446, 706.345, 852.913, 
754.906, 481.39, 646.051, 810.388, 642.572, 861.009, 748.621, 
639.78, 399.015, 701.218, 703.173, 550.613, 300.233, 623.205, 
592.083, 973.557, 578.921, 841.674, 520.968, 967.669, 504.596, 
848.008, 425.276, 512.706, 479.482, 1012.838, 572.114, 974.312, 
1072.631, 455.456, 519.14, 892.812, 903.571, 402.698, 962.713, 
641.375, 573.334, 561.29, 788.528, 749.754, 811.027, 633.2, 622.205, 
702.616, 691.318, 662.014, 652.332, 596.793, 624.849, 734.539, 
593.634, 711.332, 837.786, 837.174, 729.192, 339.278, 460.763, 
351.653, 452.46, 623.62, 459.037, 546.264, 576.723, 514.998, 
532.17, 496.179, 592.238, 473.827, 610.864, 583.741, 623.882, 
347.613, 631.51, 489.451, 609.952), pressure = c(113, 38, 184.6, 
56.9, 136.4, 6, 97.7, 44.2, 103.3, 148.7, 74.6, 150.1, 184.4, 
1.5, 158.4, 88.9, 18.4, 137.7, 179.3, 127.5, 199.1, 106.3, 138.9, 
149.3, 224.6, 174.2, 45, 117.9, 202.5, 116.5, 228.8, 171.1, 114.8, 
32.9, 146.2, 147.3, 70.7, 8.9, 106.6, 91.1, 159.7, 57, 107.2, 
44.4, 157, 39.4, 110.1, 16.2, 41.9, 32, 177.2, 54.2, 160, 204.4, 
25.3, 43.7, 122.9, 128.1, 10, 154.9, 102.2, 63.7, 57.5, 205, 
176.6, 220.9, 97.9, 90.9, 141.2, 133.7, 111.6, 104.8, 77.1, 93.3, 
165.3, 74.8, 148, 241, 240.6, 161, 2.4, 35.9, 5.5, 33.5, 102.5, 
35.2, 64, 79.9, 51.1, 57.6, 45.9, 87.2, 39.4, 96.2, 83.1, 102.6, 
4.6, 103.7, 43.7, 95.5), salinity = c(34.0021, 31.6774, 34.5094, 
32.3685, 34.2894, 30.8744, 33.5566, 31.8975, 33.7382, 34.4118, 
32.7152, 34.4237, 34.5106, 29.9942, 34.4352, 33.2292, 31.1503, 
34.3083, 34.4846, 34.1854, 34.4369, 33.1932, 33.8883, 33.9198, 
34.3934, 34.3459, 31.7398, 33.3424, 34.4308, 33.3252, 34.4925, 
34.3933, 33.3061, 31.3615, 33.8972, 33.9134, 32.1975, 30.7081, 
33.1978, 32.8666, 34.2729, 32.1129, 33.5666, 31.9104, 34.2549, 
31.8055, 33.6433, 31.4545, 31.8601, 31.7583, 34.361, 32.0123, 
34.275, 34.4879, 31.6112, 31.8972, 33.97, 34.0045, 31.1133, 34.2373, 
33.4018, 32.3235, 32.1825, 34.5804, 34.4635, 34.6194, 33.3097, 
33.0372, 34.1388, 34.0022, 33.5865, 33.4991, 32.5989, 33.0931, 
34.389, 32.5536, 34.2192, 34.7182, 34.7206, 34.3588, 32.3833, 
34.1436, 32.4341, 33.9433, 34.8319, 34.0962, 34.6193, 34.7353, 
34.4179, 34.5012, 34.3852, 34.7846, 34.3254, 34.8077, 34.7617, 
34.8327, 32.5297, 34.8285, 34.376, 34.808), temperature = c(-0.6152, 
1.4841, 0.2592, 0.0303, -0.158, 3.2785, -0.169, 0.9393, -0.3919, 
0.0577, -0.4326, 0.0769, 0.261, 2.6973, 0.0857, -0.3978, 3.3982, 
-0.135, 0.2018, -0.3561, 0.1046, -0.1562, -0.5101, -0.4656, 0.0488, 
-0.0882, 1.2957, -0.2104, 0.0969, -0.2621, 0.2628, -0.0559, -0.2369, 
2.4714, -0.406, -0.437, 0.3249, NA, -0.1641, -0.0789, -0.47, 
0.756, -0.8828, 2.4705, -0.5365, 2.4699, -0.9683, 2.9347, 2.5205, 
2.6009, -0.1366, 1.2817, -0.4399, 0.2309, 2.6902, 2.543, -0.9118, 
-0.8616, 4.2761, -0.5579, -1.1159, -0.8479, -0.3824, 0.4409, 
0.2063, 0.5677, -1.1191, -1.1866, -0.443, -0.6549, -1.1246, -1.1061, 
-1.1288, -1.1559, 0.0047, -1.0828, -0.3258, 0.8222, 0.8236, -0.0523, 
3.2471, 0.4768, 2.9768, 0.7298, 1.6138, 0.5368, 1.2708, 1.35, 
0.2793, 0.5528, 0.1167, 1.4538, 0.0896, 1.4715, 1.4861, 1.6143, 
2.8048, 1.5839, 0.0867, 1.4822), oxygen = c(337.545, 367.364, 
322.872, 368.322, 328.421, 349.408, 338.632, 370.148, 339.29, 
325.464, 358.865, 325.334, 322.871, 347.288, 325.591, 348.858, 
350.136, 327.965, 323.802, 331.916, 325.361, 339.888, 336.943, 
335.789, 325.096, 326.975, 369.327, 338.758, 325.525, 339.65, 
323.128, 327.99, 339.832, 353.153, 335.593, 335.737, 367.694, 
341.112, 339.836, 342.282, 330.574, 376.101, 349.291, 351.485, 
331.668, 350.004, 348.412, 350.937, 349.344, 349.255, 326.229, 
368.556, 330.455, 323.135, 350.703, 350.376, 339.386, 338.41, 
335.857, 332.858, 355.265, 376.756, 381.434, 322.916, 324.064, 
321.209, 358.605, 360.987, 333.247, 336.25, 349.094, 353.014, 
370.413, 359.912, 326.451, 371.269, 331.276, 317.297, 317.358, 
327.417, 341.111, 355.609, 345.333, 356.122, 313.398, 355.714, 
337.928, 315.549, 336.169, 330.109, 339.917, 314.801, 345.773, 
314.755, 315.228, 313.394, 344.535, 313.625, 340.823, 314.736
), fluorescence = c(0.13582, 0.49768, 0.089843, 0.48314, 0.096034, 
0.33488, 0.18259, 0.52033, 0.17001, 0.086226, 0.27621, 0.085777, 
0.089697, 0.18525, 0.086062, 0.23191, 0.57132, 0.093435, 0.088659, 
0.10281, 0.1631, 0.2381, 0.23751, 0.25107, 0.18158, 0.18741, 
0.51558, 0.26474, 0.16282, 0.26634, 0.17626, 0.19585, 0.26523, 
0.58539, 0.25078, 0.25052, 0.60007, 0.33444, 0.24041, 0.18822, 
0.10023, 1.1625, 0.22274, 0.69517, 0.10196, 0.75979, 0.21474, 
0.15106, 0.74221, 0.62242, 0.096315, 1.1579, 0.10027, 0.094258, 
0.54397, 0.69144, 0.12358, 0.11762, 0.0972, 0.10424, 0.17008, 
0.3403, 0.5039, 0.08105, 0.08942, 0.082081, 0.16438, 0.16498, 
0.10958, 0.118, 0.1448, 0.15927, 0.20979, 0.1576, 0.092388, 0.21511, 
0.10373, 0.077901, 0.078191, 0.093212, 0.91205, 1.0397, 1.3085, 
1.4831, 0.1472, 1.0851, 0.42351, 0.16115, 0.65351, 0.43615, 0.80317, 
0.16377, 0.8469, 0.15248, 0.16177, 0.14718, 1.1776, 0.15466, 
0.82887, 0.15303), turbidity = c(0.17, 0.161, 301.377, 0.16, 
0.169, 0.162, 0.168, 0.16, 0.166, 0.181, 0.164, 0.203, 301.355, 
0.161, 11.207, 0.163, 0.162, 0.17, 300.808, 0.169, 142.474, 280.32, 
294.107, 412.521, 217.594, 209.417, 1.633, 376.637, 145.565, 
355.119, 194.673, 239.958, 352.185, 1.38, 398.476, 403.638, 2.59, 
0.91, 291.713, 33.412, 1.98, 0.663, 1.303, 0.473, 1.896, 0.48, 
1.219, 0.531, 0.478, 0.489, 2.195, 0.583, 1.987, 2.197, 0.497, 
0.474, 1.389, 1.467, 0.659, 1.689, 0.49, 0.454, 0.469, 1.695, 
1.636, 1.818, 0.499, 0.497, 0.531, 0.507, 0.487, 0.483, 0.494, 
0.502, 1.39, 0.488, 0.566, 1.625, 1.635, 1.504, 0.56, 0.508, 
0.544, 0.52, 0.453, 0.51, 0.47, 0.456, 0.477, 0.463, 0.483, 0.458, 
0.499, 0.455, 0.46, 0.453, 0.543, 0.448, 0.486, 0.456), conductivity = c(2.782296, 
2.771939, 2.897382, 2.709914, 2.843111, 2.849715, 2.784956, 2.745975, 
2.780649, 2.871183, 2.699322, 2.873785, 2.897583, 2.72913, 2.875768, 
2.741513, 2.882021, 2.846524, 2.89035, 2.818267, 2.8794, 2.759328, 
2.783708, 2.79025, 2.872669, 2.855249, 2.762266, 2.766705, 2.878477, 
2.761088, 2.898471, 2.859664, 2.761678, 2.825838, 2.793358, 2.792059, 
2.721301, 2.870179, 2.759041, 2.740407, 2.816807, 2.747409, 2.727603, 
2.870428, 2.809681, 2.86206, 2.726242, 2.869654, 2.870779, 2.868535, 
2.852235, 2.783016, 2.819512, 2.894262, 2.863443, 2.875596, 2.755665, 
2.762612, 2.949576, 2.806478, 2.696229, 2.636537, 2.66247, 2.919256, 
2.88905, 2.93382, 2.688962, 2.66326, 2.8081, 2.780012, 2.709447, 
2.704229, 2.635064, 2.669936, 2.865751, 2.635251, 2.824332, 2.964296, 
2.964491, 2.858455, 2.971369, 2.880786, 2.954346, 2.886869, 3.035348, 
2.882372, 2.987235, 3.00375, 2.886011, 2.91614, 2.869413, 3.016904, 
2.862212, 3.020725, 3.017806, 3.035456, 2.947216, 3.032491, 2.866119, 
3.021633), par = c(2627.7, 2627.5, 2627.6, 2627.6, 2627.7, 2627.7, 
2627.7, 2627.6, 2627.7, 2627.7, 2627.7, 2627.7, 2627.7, 2600, 
2627.7, 2627.8, 2627.6, 2627.7, 2627.8, 2627.7, 2627.7, 2627.7, 
2627.7, 2627.7, 2627.7, 2627.6, 2627.5, 2627.7, 2627.7, 2627.7, 
2627.6, 2627.6, 2627.7, 2627.5, 2627.6, 2627.6, 2627.5, 2627.6, 
2627.7, 2627.7, 2627.7, 2627.3, 2627.8, 2627.5, 2627.8, 2626.8, 
2627.7, 2627.7, 2627.5, 2627.6, 2627.7, 2627.4, 2627.7, 2627.7, 
2627.6, 2627.5, 2627.7, 2627.8, 2627.7, 2627.7, 2627.7, 2627.8, 
2627.6, 2627.7, 2627.7, 2627.7, 2627.8, 2627.7, 2627.7, 2627.7, 
2627.6, 2627.7, 2627.7, 2627.7, 2627.7, 2627.7, 2627.7, 2627.7, 
2627.7, 2627.6, 2627.4, 2627.4, 2627.2, 2627.2, 2627.7, 2627.3, 
2627.5, 2627.7, 2627.5, 2627.6, 2627.4, 2627.7, 2627.4, 2627.7, 
2627.7, 2627.7, 2627.3, 2627.8, 2627.4, 2627.7), Station = c("1", 
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
"1", "1", "1", "1", "1", "1", "2", "2", "2", "2", "2", "2", "2", 
"2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", 
"3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", 
"3", "3", "3", "3", "3", "3", "3", "4", "4", "4", "4", "4", "4", 
"4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", 
"4", "5", "5", "5", "5", "5", "5", "5", "5", "5", "5", "5", "5", 
"5", "5", "5", "5", "5", "5", "5", "5"), Transect = c(1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), row.names = c(NA, -100L
), class = c("tbl_df", "tbl", "data.frame"))

That I transform in ctd file

library(oce)
library(tidyverse)
ctd.df <- df %>% as.ctd(.,salinity, temperature, pressure)

And after I apply the following code given by this website:

## preallocate the container to store the individual 
casts <- list()

### Loop the CNV file and create a list of casted CTD
for (ctd_files in 1:length(ctd.df)) {
    casts[[ctd_files]] <- read.oce(ctd.df[ctd_files])
}

This is were the error come...

The original code is below (I do an extra step to transform the dataframe in ctd file), if that can help

files <- dir(pattern='*.cnv')
casts <- list()
for (ifile in 1:length(files)) {
    casts[[ifile]] <- read.oce(files[ifile])
}

Solution

  • I have found a way to reach the wanted result with the function mapply. By first, spliting the dataset df in a list and after transform the dataframe within the list in a ctd files.

    #Transform the dataframe in list
    spl_1 <- split(df,
                   f = list(PdataLaKo$ctd_file),
                   sep = '-', drop = TRUE)
    #Transform the list in ctd files
    spl_1 <- mapply(function(dat, nm) { 
      as.ctd(dat, salinity, temperature, pressure) 
                },
      dat = spl_1, nm = names(spl_1))
    

    It is also possible to split the list in different ctd files within the Global Environment

    list2env(spl_2, envir = .GlobalEnv)