使用R中的条件删除txt文件中的几行

时间:2018-05-04 09:53:32

标签: r

我遇到问题如何删除txt文件中的几行然后转换为带有R的csv,因为我只想从txt获取数据。 我的代码无法删除,因为它删除了包含数据日期的行

这里使用的代码

setwd("D:/tugasmaritim/")
FILES <- list.files( pattern = ".txt")
for (i in 1:length(FILES)) {
l <- readLines(FILES[i],skip=4)
l2 <- l[-sapply(grep("</PRE><H3>", l), function(x) seq(x, x + 30))]
l3 <- l2[-sapply(grep("<P>Description", l2), function(x) seq(x, x + 29))]
l4 <- l3[-sapply(grep("<HTML>", l3), function(x) seq(x, x + 3))]
write.csv(l4,row.names=FALSE,file=paste0("D:/tugasmaritim/",sub(".txt","",FILES[i]),".csv"))
}

my data looks like this

&#13;
&#13;
<HTML>
<TITLE>University of Wyoming - Radiosonde Data</TITLE>
<LINK REL="StyleSheet" HREF="/resources/select.css" TYPE="text/css">
<BODY BGCOLOR="white">
<H2>96749 WIII Jakarta Observations at 00Z 02 Oct 1995</H2>
<PRE>
-----------------------------------------------------------------------------
   PRES   HGHT   TEMP   DWPT   RELH   MIXR   DRCT   SKNT   THTA   THTE   THTV
    hPa     m      C      C      %    g/kg    deg   knot     K      K      K 
-----------------------------------------------------------------------------
 1011.0      8   23.2   22.5     96  17.30      0      0  295.4  345.3  298.5
 1000.0     98   23.6   22.4     93  17.39    105      8  296.8  347.1  299.8
  977.3    300   24.6   22.1     86  17.49    105      8  299.7  351.0  302.8
  976.0    311   24.6   22.1     86  17.50    104      8  299.8  351.2  303.0
  950.0    548   23.0   22.0     94  17.87     88     12  300.5  353.2  303.7
  944.4    600   22.6   21.8     95  17.73     85     13  300.6  352.9  303.8
  925.0    781   21.2   21.0     99  17.25     90     20  301.0  351.9  304.1
  918.0    847   20.6   20.6    100  16.95     90     23  301.0  351.0  304.1
  912.4    900   20.4   18.6     89  15.00     90     26  301.4  345.7  304.1
  897.0   1047   20.0   13.0     64  10.60     90     26  302.4  334.1  304.3
  881.2   1200   19.4   11.4     60   9.70     90     26  303.3  332.5  305.1
  850.0   1510   18.2    8.2     52   8.09     95     18  305.2  329.9  306.7
  845.0   1560   18.0    7.0     49   7.49     91     17  305.5  328.4  306.9
  810.0   1920   15.0    9.0     67   8.97     60     11  306.0  333.4  307.7
  792.9   2100   14.3    3.1     47   6.06     45      8  307.1  325.9  308.2
  765.1   2400   13.1   -6.8     24   3.01     40      8  309.0  318.7  309.5
  746.0   2612   12.2  -13.8     15   1.77     38     10  310.3  316.2  310.6
  712.0   3000   10.3  -15.0     15   1.69     35     13  312.3  318.1  312.6
  700.0   3141    9.6  -15.4     16   1.66     35     13  313.1  318.7  313.4
  653.0   3714    6.6  -16.4     18   1.63     32     12  316.0  321.6  316.3
  631.0   3995    4.8   -2.2     60   5.19     31     11  317.0  333.9  318.0
  615.3   4200    3.1   -3.9     60   4.70     30     11  317.4  332.8  318.3
  601.0   4391    1.6   -5.4     60   4.28     20      8  317.8  331.9  318.6
  592.9   4500    0.6  -12.0     38   2.59     15      6  317.9  326.6  318.4
  588.0   4567    0.0  -16.0     29   1.88     11      6  317.9  324.4  318.3
  571.0   4800   -1.2  -18.9     25   1.51    355      5  319.1  324.4  319.4
  549.8   5100   -2.8  -22.8     20   1.12     45      6  320.7  324.8  321.0
  513.0   5649   -5.7  -29.7     13   0.64    125     10  323.6  326.0  323.8
  500.0   5850   -5.1  -30.1     12   0.63    155     11  326.8  329.1  326.9
  494.0   5945   -4.9  -29.9     12   0.65    146     11  328.1  330.6  328.3
  471.7   6300   -7.4  -32.0     12   0.56    110     13  329.3  331.5  329.4
  453.7   6600   -9.6  -33.8     12   0.49    100     14  330.3  332.2  330.4
  400.0   7570  -16.5  -39.5     12   0.31    105     14  333.5  334.7  333.5
  398.0   7607  -16.9  -39.9     12   0.30    104     14  333.4  334.6  333.5
  371.9   8100  -20.4  -42.6     12   0.24     95     16  335.4  336.3  335.4
  300.0   9660  -31.3  -51.3     12   0.11    115     18  341.1  341.6  341.2
  269.0  10420  -36.3  -55.3     12   0.08     79     20  344.7  345.0  344.7
  265.9  10500  -36.9                          75     20  344.9         344.9
  250.0  10920  -40.3                          80     28  346.0         346.0
  243.4  11100  -41.8                          85     37  346.4         346.4
  222.5  11700  -46.9                          75     14  347.6         347.6
  214.0  11960  -49.1                          68     16  348.1         348.1
  200.0  12400  -52.7                          55     20  349.1         349.1
  156.0  13953  -66.1                          55     25  352.1         352.1
  152.3  14100  -67.2                          55     26  352.6         352.6
  150.0  14190  -67.9                          55     26  352.9         352.9
  144.7  14400  -69.6                          60     26  353.6         353.6
  137.5  14700  -72.0                          60     39  354.6         354.6
  130.7  15000  -74.3                          50     28  355.6         355.6
  124.2  15300  -76.7                          40     36  356.5         356.5
  118.0  15600  -79.1                          50     48  357.4         357.4
  116.0  15698  -79.9                          45     44  357.6         357.6
  112.0  15900  -79.1                          45     26  362.6         362.6
  106.3  16200  -78.0                          35     24  370.2         370.2
  100.0  16550  -76.7                          35     24  379.3         379.3
</PRE><H3>Station information and sounding indices</H3><PRE>
                         Station identifier: WIII
                             Station number: 96749
                           Observation time: 951002/0000
                           Station latitude: -6.11
                          Station longitude: 106.65
                          Station elevation: 8.0
                            Showalter index: 6.30
                               Lifted index: -1.91
    LIFT computed using virtual temperature: -2.80
                                SWEAT index: 145.41
                                    K index: 6.50
                         Cross totals index: 13.30
                      Vertical totals index: 23.30
                        Totals totals index: 36.60
      Convective Available Potential Energy: 799.02
             CAPE using virtual temperature: 1070.13
                      Convective Inhibition: -26.70
             CINS using virtual temperature: -12.88
                           Equilibrum Level: 202.64
 Equilibrum Level using virtual temperature: 202.60
                   Level of Free Convection: 828.70
             LFCT using virtual temperature: 909.19
                     Bulk Richardson Number: 210.78
          Bulk Richardson Number using CAPV: 282.30
  Temp [K] of the Lifted Condensation Level: 294.96
Pres [hPa] of the Lifted Condensation Level: 958.67
     Mean mixed layer potential temperature: 298.56
              Mean mixed layer mixing ratio: 17.50
              1000 hPa to 500 hPa thickness: 5752.00
Precipitable water [mm] for entire sounding: 36.31
</PRE>
<H2>96749 WIII Jakarta Observations at 00Z 03 Oct 1995</H2>
<PRE>
-----------------------------------------------------------------------------
   PRES   HGHT   TEMP   DWPT   RELH   MIXR   DRCT   SKNT   THTA   THTE   THTV
    hPa     m      C      C      %    g/kg    deg   knot     K      K      K 
-----------------------------------------------------------------------------
 1012.0      8   23.6   22.9     96  17.72    140      2  295.7  346.9  298.9
 1000.0    107   24.0   21.6     86  16.54    135      3  297.1  345.2  300.1
  990.0    195   24.4   20.3     78  15.39    128      4  298.4  343.4  301.2
  945.4    600   22.9   20.2     85  16.00     95      7  300.9  348.0  303.7
  925.0    791   22.2   20.1     88  16.29    100      6  302.0  350.3  304.9
  913.5    900   21.9   18.2     80  14.63    105      6  302.8  346.3  305.4
  911.0    924   21.8   17.8     78  14.28    108      6  302.9  345.4  305.5
  850.0   1522   17.4   16.7     96  14.28    175      6  304.4  347.1  307.0
  836.0   1665   16.4   16.4    100  14.24    157      7  304.8  347.5  307.4
  811.0   1925   15.0   14.7     98  13.14    123      8  305.9  345.6  308.3
  795.0   2095   14.2    7.2     63   8.08    101      9  306.8  331.6  308.3
  794.5   2100   14.2    7.2     63   8.05    100      9  306.8  331.5  308.3
  745.0   2642   10.4    2.4     58   6.14     64     11  308.4  327.6  309.6
  736.0   2744   11.0    0.0     47   5.23     57     11  310.2  326.7  311.1
  713.8   3000    9.2    5.0     75   7.70     40     12  310.9  335.0  312.4
  711.0   3033    9.0    5.6     79   8.08     40     12  311.0  336.2  312.6
  700.0   3163    8.6    1.6     61   6.18     40     12  312.0  331.5  313.1
  688.5   3300    8.3   -6.0     36   3.57     60     12  313.1  324.8  313.8
  678.0   3427    8.0  -13.0     21   2.08     70     12  314.2  321.2  314.6
  642.0   3874    5.0   -2.0     61   5.17    108     11  315.7  332.4  316.7
  633.0   3989    4.4  -11.6     30   2.50    117     10  316.3  324.7  316.8
  616.6   4200    3.1  -14.1     27   2.09    135     10  317.1  324.3  317.6
  580.0   4694    0.0  -20.0     21   1.36    164     13  319.1  323.9  319.4
  572.3   4800   -0.4  -20.7     20   1.29    170     14  319.9  324.5  320.1
  510.8   5700   -4.0  -26.6     15   0.86     80     10  326.1  329.2  326.2
  500.0   5870   -4.7  -27.7     15   0.79     80     10  327.2  330.2  327.4
  497.0   5917   -4.9  -27.9     15   0.78     71     13  327.6  330.5  327.7
  491.7   6000   -5.5  -28.3     15   0.76     55     19  327.9  330.7  328.0
  473.0   6300   -7.6  -29.9     15   0.68     55     16  328.9  331.4  329.0
  436.0   6930  -12.1  -33.1     16   0.54     77     17  330.9  333.0  331.0
  400.0   7580  -17.9  -37.9     16   0.37    100     19  331.6  333.1  331.7
  388.3   7800  -19.9  -39.9     15   0.31    105     20  331.8  333.1  331.9
  386.0   7844  -20.3  -40.3     15   0.30    103     20  331.9  333.1  331.9
  372.0   8117  -18.3  -38.3     16   0.38     91     23  338.1  339.6  338.1
  343.6   8700  -22.1  -41.4     16   0.30     65     29  340.7  342.0  340.8
  329.0   9018  -24.1  -43.1     16   0.26     73     27  342.2  343.2  342.2
  300.0   9680  -29.9  -44.9     22   0.23     90     22  343.1  344.1  343.2
  278.6  10200  -34.3                          85     37  344.1         344.1
  266.9  10500  -36.8                          60     32  344.7         344.7
  255.8  10800  -39.4                          65     27  345.2         345.2
  250.0  10960  -40.7                          65     27  345.4         345.4
  204.0  12300  -51.8                          55     23  348.6         348.6
  200.0  12430  -52.9                          55     23  348.8         348.8
  194.6  12600  -55.0                          60     23  348.1         348.1
  160.7  13800  -70.1                          35     39  342.4         342.4
  153.2  14100  -73.9                          35     41  340.6         340.6
  150.0  14230  -75.5                          35     41  339.9         339.9
  131.5  15000  -76.3                          50     53  351.6         351.6
  124.9  15300  -76.6                          50     57  356.2         356.2
  122.0  15436  -76.7                          57     45  358.3         358.3
  118.6  15600  -77.3                          65     31  360.2         360.2
  115.0  15779  -77.9                          65     31  362.2         362.2
  112.6  15900  -77.7                          85     17  364.8         364.8
  107.0  16200  -77.2                         130     10  371.2         371.2
  100.0  16590  -76.5                         120     18  379.7         379.7
</PRE><H3>Station information and sounding indices</H3><PRE>
                         Station identifier: WIII
                             Station number: 96749
                           Observation time: 951003/0000
                           Station latitude: -6.11
                          Station longitude: 106.65
                          Station elevation: 8.0
                            Showalter index: -0.58
                               Lifted index: 0.17
    LIFT computed using virtual temperature: -0.57
                                SWEAT index: 222.41
                                    K index: 31.80
                         Cross totals index: 21.40
                      Vertical totals index: 22.10
                        Totals totals index: 43.50
      Convective Available Potential Energy: 268.43
             CAPE using virtual temperature: 431.38
                      Convective Inhibition: -84.04
             CINS using virtual temperature: -81.56
                           Equilibrum Level: 141.42
 Equilibrum Level using virtual temperature: 141.35
                   Level of Free Convection: 784.91
             LFCT using virtual temperature: 804.89
                     Bulk Richardson Number: 221.19
          Bulk Richardson Number using CAPV: 355.46
  Temp [K] of the Lifted Condensation Level: 293.21
Pres [hPa] of the Lifted Condensation Level: 940.03
     Mean mixed layer potential temperature: 298.46
              Mean mixed layer mixing ratio: 16.01
              1000 hPa to 500 hPa thickness: 5763.00
Precipitable water [mm] for entire sounding: 44.54
&#13;
&#13;
&#13;

这里是我的数据

data

这就是我想要的

contoh

0 个答案:

没有答案