对具有相同结构的几个数据集使用lapply并可能进行for循环以提取和计算每个数据帧的值
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了对具有相同结构的几个数据集使用lapply并可能进行for循环以提取和计算每个数据帧的值相关的知识,希望对你有一定的参考价值。
亲爱的R恋人,我有一个问题困扰着lapply和for loops。
我有许多数据文件,它们的结构均相同,但在几行中包含值,并且用“空格分隔”。
我可以使用lapply命令读取文件,然后在lapply中使用循环或函数来从每个数据集中以不同的步骤计算值吗?
我需要在一定温度范围内找到最大值和最小值,并希望创建两个值的差。
我想为每个包含数据集名称和结果值的数据集保存的差异。
当我尝试使用一个数据集时,这没问题-我最大的挑战是将单个文件分类的命令转换为要在lapply命令中应用的函数。下面是用于读取一个数据集的代码片段,您可能了解我想要做什么:
data1<- read.fwf("data1.cur", widths = c(1, 5, 4, 5,4,6,1,5,13,3,34), skip = 1) # necessary because of the space limiting
data1<- data1[,c(2,4,6,8,9)] # then i select the coloums which i need and are resulting of the previous code nor NAs
colnames(data1) <- c("TempC", "value1", "value2", "value3", "value4") # then i give the columnames which I deleted in the first code line by skip=1, because they are not usable
plot(data1$TempC, data1$value1, type="l") # here i check if everything worked - yes it does
max<- subset(data1,data1$TempC > 220 & data1$TempC < 240) # there is the first range where the maximum should occure (there are several maxima in the data1 set but i need that maximum close to 230 degree celsius
min <- subset(data1,data1$TempC > 390 & data1$TempC < 410) # same for the maximum i search for the minimum at a distinct place around 400 degree celsius
max_real <- max(max$value1) # select the real max
min_real <- min(min$value1) # select the real min
difference <- max_real -min_real # final difference value
亲爱的R爱好者,我希望您能为我提供帮助-真的很棒-我希望我写了代码,并希望尽可能清楚地做什么。如果没有的话-请不要等着问-非常感谢大家,度过了一个愉快的周末,克里斯
Here is a single data file (data1) with spaces inbetween the columns (number of spaces is varying by the length of the values, this is why the read.fwf() command and specific widths are used to read the data:
TEMP TSUSC CSUSC NSUSC BULKS FERRT FERRB TIMEPL_20leer
26.2 21.3 82.92 0.271 1076.9 0.0 0.0 0 79.0
27.2 21.7 83.32 0.272 1082.1 0.0 0.0 24 0.770
32.9 21.8 83.31 0.272 1081.9 0.0 0.0 47 0.000
40.4 22.4 83.67 0.273 1086.6 0.0 0.0 69 0.000
48.2 23.3 84.53 0.276 1097.8 0.0 0.0 92 5
55.5 23.3 84.83 0.277 1101.6 0.0 0.0 115 0/1400
62.5 25.3 86.89 0.284 1128.5 0.0 0.0 138 2
68.8 25.0 86.59 0.283 1124.5 0.0 0.0 161 0
74.8 25.1 86.70 0.283 1126.0 0.0 0.0 183 0
80.8 25.0 86.78 0.284 1127.0 0.0 0.0 206 0.00
86.3 26.1 87.91 0.287 1141.7 0.0 0.0 229 0.00
91.8 26.3 88.05 0.288 1143.5 0.0 0.0 252 0.00
97.4 27.6 89.44 0.292 1161.5 0.0 0.0 275 0.00
102.4 27.0 89.03 0.291 1156.2 0.0 0.0 297 0.00
107.7 27.2 89.17 0.291 1158.1 0.0 0.0 320 0.00
112.7 28.2 90.13 0.295 1170.5 0.0 0.0 343 0.00
117.2 28.8 90.69 0.296 1177.8 0.0 0.0 366 0.00
121.4 28.9 90.70 0.296 1177.9 0.0 0.0 389 0.00
125.9 28.7 90.42 0.295 1174.2 0.0 0.0 412 0.00
130.5 29.1 90.93 0.297 1181.0 0.0 0.0 434
135.5 28.3 90.09 0.294 1170.0 0.0 0.0 457
140.6 28.4 90.07 0.294 1169.8 0.0 0.0 480
146.2 29.2 90.95 0.297 1181.2 0.0 0.0 503 177.571
151.8 29.3 91.10 0.298 1183.2 0.0 0.0 526 292.015
157.7 29.6 91.08 0.298 1182.9 0.0 0.0 549 174
163.4 29.7 91.30 0.298 1185.7 0.0 0.0 572 1.00
168.8 28.9 90.68 0.296 1177.7 0.0 0.0 594 40
174.2 29.5 91.16 0.298 1183.9 0.0 0.0 617 408
179.8 29.6 91.17 0.298 1184.0 0.0 0.0 640 0
185.0 30.5 92.23 0.301 1197.8 0.0 0.0 663 0.000
190.7 30.8 92.71 0.303 1204.0 0.0 0.0 686 0.000
195.8 31.3 93.17 0.305 1210.1 0.0 0.0 709 0
201.0 31.8 93.68 0.306 1216.6 0.0 0.0 732 0.00
206.2 32.3 94.23 0.308 1223.8 0.0 0.0 755 0
211.3 33.5 95.51 0.312 1240.4 0.0 0.0 778 0
216.8 34.3 96.34 0.315 1251.1 0.0 0.0 800 0
222.6 35.0 97.18 0.318 1262.1 0.0 0.0 823 0
228.3 35.9 98.20 0.321 1275.3 0.0 0.0 846 0.0
234.1 36.9 99.40 0.325 1290.9 0.0 0.0 869 0.0
240.1 37.1 99.53 0.325 1292.6 0.0 0.0 892 0.0
245.7 36.9 99.24 0.324 1288.9 0.0 0.0 915 0.0
251.5 36.7 98.96 0.323 1285.2 0.0 0.0 938 0.0
257.5 36.0 98.15 0.321 1274.6 0.0 0.0 961 0.0
263.1 35.6 97.50 0.319 1266.2 0.0 0.0 984 0.0
268.6 35.0 97.04 0.317 1260.2 0.0 0.0 1007 0.0
274.2 33.4 95.45 0.312 1239.6 0.0 0.0 1030 0.0
279.5 32.5 94.62 0.309 1228.9 0.0 0.0 1053 0.0
285.1 31.2 93.36 0.305 1212.5 0.0 0.0 1076
290.7 29.1 91.30 0.298 1185.7 0.0 0.0 1100
296.0 27.9 89.98 0.294 1168.6 0.0 0.0 1123
301.6 25.9 88.12 0.288 1144.4 0.0 0.0 1146
307.0 24.2 86.48 0.283 1123.1 0.0 0.0 1169
312.1 22.6 85.07 0.278 1104.8 0.0 0.0 1192
317.4 21.3 83.75 0.274 1087.6 0.0 0.0 1215
322.8 19.5 82.02 0.268 1065.2 0.0 0.0 1238
328.2 18.3 80.68 0.264 1047.8 0.0 0.0 1261
333.6 16.9 79.22 0.259 1028.8 0.0 0.0 1284
339.2 15.6 77.89 0.255 1011.6 0.0 0.0 1307
344.7 13.6 76.03 0.248 987.4 0.0 0.0 1330
350.4 12.3 74.68 0.244 969.9 0.0 0.0 1353
355.8 11.1 73.63 0.241 956.3 0.0 0.0 1376
361.5 9.7 72.35 0.236 939.7 0.0 0.0 1399
366.7 7.9 70.53 0.231 916.0 0.0 0.0 1422
372.4 6.8 69.36 0.227 900.8 0.0 0.0 1446
377.6 5.1 67.79 0.222 880.4 0.0 0.0 1469
382.8 3.6 66.49 0.217 863.5 0.0 0.0 1492
387.7 2.1 64.99 0.212 844.0 0.0 0.0 1516
393.2 0.4 63.31 0.207 822.2 0.0 0.0 1539
398.7 -0.8 62.12 0.203 806.7 0.0 0.0 1562
404.3 -2.1 60.96 0.199 791.6 0.0 0.0 1584
409.5 -3.4 59.66 0.195 774.7 0.0 0.0 1608
414.7 -4.5 58.41 0.191 758.6 0.0 0.0 1630
419.7 -5.5 57.50 0.188 746.7 0.0 0.0 1653
424.7 -6.3 56.82 0.186 737.9 0.0 0.0 1676
430.2 -7.0 56.06 0.183 728.1 0.0 0.0 1699
435.2 -7.7 55.25 0.181 717.5 0.0 0.0 1722
440.5 -8.1 54.98 0.180 714.0 0.0 0.0 1745
445.8 -8.2 54.96 0.180 713.8 0.0 0.0 1769
451.7 -8.1 55.14 0.180 716.1 0.0 0.0 1791
457.7 -7.4 55.92 0.183 726.2 0.0 0.0 1814
463.0 -6.0 57.24 0.187 743.4 0.0 0.0 1837
468.6 -4.0 59.28 0.194 769.8 0.0 0.0 1860
474.9 -1.2 62.12 0.203 806.8 0.0 0.0 1883
480.2 1.8 65.14 0.213 846.0 0.0 0.0 1906
485.6 5.2 68.63 0.224 891.3 0.0 0.0 1929
491.0 8.2 71.64 0.234 930.4 0.0 0.0 1952
495.8 11.5 74.90 0.245 972.7 0.0 0.0 1975
501.2 13.9 77.32 0.253 1004.2 0.0 0.0 1998
506.3 15.7 79.14 0.259 1027.8 0.0 0.0 2021
511.8 16.6 80.10 0.262 1040.3 0.0 0.0 2044
517.5 16.5 80.06 0.262 1039.7 0.0 0.0 2067
522.6 15.6 79.22 0.259 1028.8 0.0 0.0 2090
527.5 13.8 77.43 0.253 1005.6 0.0 0.0 2113
532.4 11.5 75.20 0.246 976.7 0.0 0.0 2135
536.9 7.7 71.36 0.233 926.8 0.0 0.0 2158
542.4 3.2 66.90 0.219 868.8 0.0 0.0 2181
547.6 -2.3 61.53 0.201 799.0 0.0 0.0 2204
552.8 -7.6 56.35 0.184 731.9 0.0 0.0 2227
558.0 -12.4 51.46 0.168 668.3 0.0 0.0 2250
563.2 -17.2 46.85 0.153 608.4 0.0 0.0 2273
568.8 -21.7 42.37 0.138 550.3 0.0 0.0 2296
574.6 -25.8 38.33 0.125 497.8 0.0 0.0 2319
580.2 -31.4 32.76 0.107 425.4 0.0 0.0 2342
585.4 -37.2 27.00 0.088 350.6 0.0 0.0 2365
590.4 -40.3 23.76 0.078 308.5 0.0 0.0 2387
595.4 -42.4 21.63 0.071 280.9 0.0 0.0 2410
600.6 -44.3 19.69 0.064 255.7 0.0 0.0 2433
605.6 -45.6 18.38 0.060 238.8 0.0 0.0 2456
610.9 -46.8 17.30 0.057 224.7 0.0 0.0 2479
615.0 -47.8 16.26 0.053 211.2 0.0 0.0 2502
620.0 -48.3 15.90 0.052 206.5 0.0 0.0 2525
624.4 -49.3 14.97 0.049 194.4 0.0 0.0 2548
629.1 -50.1 14.24 0.047 185.0 0.0 0.0 2571
634.2 -51.0 13.50 0.044 175.3 0.0 0.0 2594
639.5 -51.5 13.18 0.043 171.1 0.0 0.0 2617
644.6 -52.4 12.21 0.040 158.6 0.0 0.0 2639
649.7 -53.1 11.54 0.038 149.8 0.0 0.0 2662
654.7 -53.5 11.16 0.036 144.9 0.0 0.0 2685
659.8 -54.6 9.97 0.033 129.5 0.0 0.0 2708
664.6 -55.1 9.60 0.031 124.7 0.0 0.0 2731
669.4 -55.6 9.12 0.030 118.5 0.0 0.0 2754
674.5 -56.2 8.59 0.028 111.5 0.0 0.0 2777
679.3 -56.7 7.98 0.026 103.6 0.0 0.0 2800
684.4 -57.2 7.45 0.024 96.7 0.0 0.0 2823
689.6 -57.8 6.86 0.022 89.1 0.0 0.0 2846
695.1 -58.5 6.22 0.020 80.8 0.0 0.0 2869
699.3 -59.3 5.53 0.018 71.8 0.0 0.0 2892
705.4 -59.7 5.32 0.017 69.0 0.0 0.0 2915
706.7 -60.1 4.96 0.016 64.4 0.0 0.0 2938
706.0 -60.1 4.94 0.016 64.2 0.0 0.0 2961
704.4 -60.1 4.92 0.016 63.9 0.0 0.0 2984
702.2 -59.9 5.14 0.017 66.8 0.0 0.0 3007
698.9 -59.4 5.64 0.018 73.2 0.0 0.0 3030
695.4 -59.0 6.04 0.020 78.4 0.0 0.0 3053
691.5 -58.4 6.64 0.022 86.2 0.0 0.0 3076
687.0 -57.9 7.03 0.023 91.4 0.0 0.0 3099
683.5 -57.0 7.83 0.026 101.7 0.0 0.0 3122
679.0 -56.3 8.52 0.028 110.7 0.0 0.0 3145
673.9 -55.2 9.58 0.031 124.4 0.0 0.0 3168
669.1 -54.0 10.74 0.035 139.5 0.0 0.0 3191
664.6 -52.8 12.05 0.039 156.6 0.0 0.0 3214
660.1 -51.8 13.17 0.043 171.0 0.0 0.0 3237
656.0 -50.4 14.54 0.048 188.8 0.0 0.0 3260
651.6 -49.1 15.78 0.052 205.0 0.0 0.0 3283
647.8 -47.5 17.37 0.057 225.5 0.0 0.0 3306
643.0 -45.5 19.12 0.062 248.3 0.0 0.0 3329
638.6 -43.6 20.93 0.068 271.8 0.0 0.0 3351
634.2 -41.8 22.57 0.074 293.2 0.0 0.0 3374
630.1 -39.5 24.73 0.081 321.2 0.0 0.0 3397
625.7 -37.6 26.50 0.087 344.1 0.0 0.0 3420
621.0 -35.2 28.95 0.095 375.9 0.0 0.0 3443
616.3 -32.6 31.39 0.103 407.6 0.0 0.0 3466
612.5 -30.0 33.94 0.111 440.8 0.0 0.0 3489
607.8 -27.2 36.75 0.120 477.3 0.0 0.0 3512
603.1 -24.0 39.93 0.131 518.6 0.0 0.0 3535
598.5 -20.3 43.57 0.142 565.8 0.0 0.0 3558
594.1 -16.5 47.38 0.155 615.3 0.0 0.0 3581
589.5 -11.1 52.78 0.172 685.5 0.0 0.0 3604
585.1 -3.6 60.24 0.197 782.4 0.0 0.0 3626
580.5 5.8 69.59 0.227 903.8 0.0 0.0 3649
575.8 16.1 79.82 0.261 1036.6 0.0 0.0 3672
571.5 25.0 88.73 0.290 1152.3 0.0 0.0 3695
566.9 33.8 97.44 0.318 1265.5 0.0 0.0 3718
562.0 43.0 106.65 0.349 1385.0 0.0 0.0 3741
557.1 53.2 116.72 0.381 1515.9 0.0 0.0 3764
551.9 63.8 127.34 0.416 1653.8 0.0 0.0 3787
546.7 73.9 137.42 0.449 1784.7 0.0 0.0 3809
541.5 83.8 147.30 0.481 1913.0 0.0 0.0 3833
536.6 93.6 157.10 0.513 2040.3 0.0 0.0 3855
531.7 102.6 166.08 0.543 2156.9 0.0 0.0 3878
527.5 111.0 174.46 0.570 2265.8 0.0 0.0 3901
522.6 119.5 182.91 0.598 2375.4 0.0 0.0 3924
518.1 127.8 191.17 0.625 2482.7 0.0 0.0 3947
513.3 137.0 200.36 0.655 2602.1 0.0 0.0 3970
507.8 146.5 209.80 0.686 2724.7 0.0 0.0 3994
502.7 156.5 219.84 0.718 2855.1 0.0 0.0 4018
497.3 165.2 228.50 0.747 2967.6 0.0 0.0 4042
492.5 173.1 236.42 0.773 3070.4 0.0 0.0 4066
486.8 180.5 243.86 0.797 3167.0 0.0 0.0 4090
481.7 189.7 253.04 0.827 3286.2 0.0 0.0 4114
476.7 197.0 260.23 0.850 3379.6 0.0 0.0 4138
471.3 203.2 266.36 0.871 3459.3 0.0 0.0 4162
466.0 208.5 271.60 0.888 3527.3 0.0 0.0 4186
460.9 214.4 277.46 0.907 3603.4 0.0 0.0 4210
455.6 219.4 282.44 0.923 3668.1 0.0 0.0 4234
450.3 224.3 287.36 0.939 3732.0 0.0 0.0 4258
445.0 228.1 291.14 0.951 3781.0 0.0 0.0 4282
439.4 230.3 293.25 0.958 3808.5 0.0 0.0 4306
434.1 234.1 297.02 0.971 3857.3 0.0 0.0 4330
428.8 236.1 298.97 0.977 3882.8 0.0 0.0 4354
423.5 237.6 300.43 0.982 3901.7 0.0 0.0 4378
418.0 238.0 300.82 0.983 3906.8 0.0 0.0 4403
412.7 240.2 303.11 0.991 3936.5 0.0 0.0 4427
407.7 241.5 304.38 0.995 3953.0 0.0 0.0 4451
402.8 241.9 304.78 0.996 3958.2 0.0 0.0 4475
397.3 241.6 304.48 0.995 3954.3 0.0 0.0 4499
392.4 241.9 304.78 0.996 3958.2 0.0 0.0 4523
387.1 241.9 304.68 0.996 3956.9 0.0 0.0 4547
381.9 243.2 305.98 1.000 3973.8 0.0 0.0 4571
376.5 242.4 305.09 0.997 3962.2 0.0 0.0 4595
371.3 243.0 305.60 0.999 3968.8 0.0 0.0 4620
365.8 243.3 305.92 1.000 3973.0 0.0 0.0 4644
360.4 243.2 305.73 0.999 3970.5 0.0 0.0 4667
354.9 242.3 304.88 0.996 3959.5 0.0 0.0 4692
348.9 240.6 303.24 0.991 3938.2 0.0 0.0 4716
343.8 241.9 304.52 0.995 3954.8 0.0 0.0 4739
338.1 240.4 303.04 0.990 3935.6 0.0 0.0 4764
332.1 237.3 299.86 0.980 3894.2 0.0 0.0 4788
326.8 237.7 300.31 0.981 3900.1 0.0 0.0 4811
320.8 236.7 299.15 0.978 3885.1 0.0 0.0 4836
314.9 236.2 298.64 0.976 3878.4 0.0 0.0 4859
309.5 233.9 296.36 0.969 3848.8 0.0 0.0 4883
303.9 233.1 295.53 0.966 3838.0 0.0 0.0 4907
298.0 231.6 293.83 0.960 3815.9 0.0 0.0 4931
292.4 230.9 293.40 0.959 3810.4 0.0 0.0 4955
286.8 228.5 290.78 0.950 3776.4 0.0 0.0 4979
281.7 227.0 289.32 0.946 3757.4 0.0 0.0 5002
276.2 226.5 288.86 0.944 3751.4 0.0 0.0 5026
270.9 225.5 287.80 0.941 3737.6 0.0 0.0 5050
265.3 222.1 284.46 0.930 3694.3 0.0 0.0 5074
260.0 222.8 285.21 0.932 3704.1 0.0 0.0 5098
254.5 219.5 281.88 0.921 3660.8 0.0 0.0 5121
248.7 216.8 279.16 0.912 3625.4 0.0 0.0 5145
243.4 216.3 278.66 0.911 3619.0 0.0 0.0 5169
237.7 217.9 280.31 0.916 3640.4 0.0 0.0 5193
231.9 212.7 275.13 0.899 3573.1 0.0 0.0 5217
226.4 213.3 275.43 0.900 3577.0 0.0 0.0 5241
220.9 208.5 270.69 0.885 3515.4 0.0 0.0 5264
215.7 208.5 270.73 0.885 3516.0 0.0 0.0 5288
210.0 207.5 269.49 0.881 3499.8 0.0 0.0 5312
204.5 206.8 268.74 0.878 3490.2 0.0 0.0 5336
198.8 204.3 266.34 0.870 3458.9 0.0 0.0 5360
193.4 198.5 260.48 0.851 3382.9 0.0 0.0 5384
187.7 202.7 264.58 0.865 3436.1 0.0 0.0 5408
182.0 201.6 263.64 0.862 3423.9 0.0 0.0 5431
176.9 200.1 262.23 0.857 3405.6 0.0 0.0 5455
171.2 196.4 258.52 0.845 3357.4 0.0 0.0 5479
166.3 202.2 264.59 0.865 3436.2 0.0 0.0 5503
161.2 199.0 261.43 0.854 3395.2 0.0 0.0 5527
156.1 192.2 254.32 0.831 3302.8 0.0 0.0 5551
150.8 195.8 257.96 0.843 3350.1 0.0 0.0 5575
145.9 192.9 255.13 0.834 3313.4 0.0 0.0 5598
141.1 194.2 255.91 0.836 3323.5 0.0 0.0 5622
136.1 189.1 250.93 0.820 3258.8 0.0 0.0 5646
131.0 190.9 252.85 0.826 3283.8 0.0 0.0 5670
125.9 187.4 249.32 0.815 3237.9 0.0 0.0 5694
121.2 187.4 249.17 0.814 3236.0 0.0 0.0 5718
116.4 188.3 250.25 0.818 3250.0 0.0 0.0 5742
111.4 184.4 246.32 0.805 3198.9 0.0 0.0 5766
106.9 183.7 245.49 0.802 3188.1 0.0 0.0 5790
102.1 181.4 243.50 0.796 3162.4 0.0 0.0 5814
97.6 181.2 243.32 0.795 3160.0 0.0 0.0 5838
92.9 180.9 243.16 0.795 3158.0 0.0 0.0 5862
88.4 179.4 241.56 0.789 3137.2 0.0 0.0 5886
84.2 185.0 247.29 0.808 3211.6 0.0 0.0 5910
79.8 178.9 240.75 0.787 3126.6 0.0 0.0 5934
75.6 178.4 240.44 0.786 3122.6 0.0 0.0 5957
71.4 179.4 241.48 0.789 3136.1 0.0 0.0 5981
67.5 176.1 238.13 0.778 3092.6 0.0 0.0 6005
63.8 175.3 237.31 0.776 3081.9 0.0 0.0 6029
59.9 175.4 237.26 0.775 3081.3 0.0 0.0 6053
56.3 174.9 236.79 0.774 3075.2 0.0 0.0 6077
52.9 172.3 234.11 0.765 3040.3 0.0 0.0 6100
50.0 175.3 237.08 0.775 3079.0 0.0 0.0 6124
47.2 172.4 234.26 0.766 3042.3 0.0 0.0 6148
44.3 172.3 234.14 0.765 3040.8 0.0 0.0 6172
41.7 171.2 233.04 0.762 3026.5 0.0 0.0 6196
39.1 169.9 231.74 0.757 3009.6 0.0 0.0 6220
答案
将要应用到代码的所有代码放入函数中的每个文件。
do_analaysis <- function(filename) {
data1<- read.fwf(filename, widths = c(1, 5, 4, 5,4,6,1,5,13,3,34), skip = 1)
data1<- data1[,c(2,4,6,8,9)]
colnames(data1) <- c("TempC", "value1", "value2", "value3", "value4")
max<- subset(data1,data1$TempC > 220 & data1$TempC < 240)
min <- subset(data1,data1$TempC > 390 & data1$TempC < 410)
max_real <- max(max$value1)
min_real <- min(min$value1)
return(max_real - min_real )
}
使用list.files
收集所有文件名。
filenames <- list.files('path/to/files/', pattern = '\.cur', full.names = TRUE)
并使用lapply
应用功能:
output <- lapply(filenames, do_analaysis)
以上是关于对具有相同结构的几个数据集使用lapply并可能进行for循环以提取和计算每个数据帧的值的主要内容,如果未能解决你的问题,请参考以下文章