Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从R中的文本文件中提取表(和其他信息)_R_Regex_Text_Text Extraction_Data Extraction - Fatal编程技术网

从R中的文本文件中提取表(和其他信息)

从R中的文本文件中提取表(和其他信息),r,regex,text,text-extraction,data-extraction,R,Regex,Text,Text Extraction,Data Extraction,我正试图使用R从中提取数据表和其他一些信息,但是尽管花了整个晚上在StackOverflow上,仍然遇到了问题 例如,以下是以下数据: 到目前为止,我管理的最好的方法是使用sed(R之外)删除*d和#'d变量,但使用read.table导入此变量(lowstoftdata.text,skip=8,col.names=c(“年”、“月”、“最高温度”、“最低温度”、“霜冻”、“降雨”、“阳光”))当它触及标记为临时的2020年以后的数据时就会失效。提取纬度和经度值也非常方便,通常位于第2行,但如果

我正试图使用R从中提取数据表和其他一些信息,但是尽管花了整个晚上在StackOverflow上,仍然遇到了问题

例如,以下是以下数据:

到目前为止,我管理的最好的方法是使用
sed
(R之外)删除*d和#'d变量,但使用
read.table导入此变量(lowstoftdata.text,skip=8,col.names=c(“年”、“月”、“最高温度”、“最低温度”、“霜冻”、“降雨”、“阳光”))
当它触及标记为临时的2020年以后的数据时就会失效。提取纬度和经度值也非常方便,通常位于第2行,但如果像Lowstoft一样,站点在某个点移动,则可以位于第3行,但我有限的正则表达式知识(以及移动的目标)让我失望

我的伪代码方法是:

  • 用纬度和经度标识线,解析该线以提取这些变量
  • 识别以数字开头的第一行,并从该行读取.table

  • 。。。但事实证明,将其转化为实践是一项挑战,因为我处理格式良好的CSV文件以外的任何事情的经验有限,因此,如果您能就如何开始提供任何建议,我将不胜感激。

    以下是一种“解析”标题文本请求的方法:

    metadata <- 
     readLines(url("https://www.metoffice.gov.uk/pub/data/weather/uk/climate/stationdata/lowestoftdata.txt"), n=9)
    > metadata
    [1] "Lowestoft / Lowestoft Monckton Ave from Sept 2007"                                                                                        
    [2] "Location 654300E 294600N 25m amsl to July 2007 "                                                                                          
    [3] "& from Sept 2007 653000E 293800N, Lat 52.483 Lon 1.727, 18m amsl"                                                                         
    [4] "Estimated data is marked with a * after the value."                                                                                       
    [5] "Missing data (more than 2 days missing in month) is marked by  ---."                                                                      
    [6] "Sunshine data taken from an automatic Kipp & Zonen sensor marked with a #, otherwise sunshine data taken from a Campbell Stokes recorder."
    [7] "   yyyy  mm   tmax    tmin      af    rain     sun"                                                                                       
    [8] "              degC    degC    days      mm   hours"  
    
                                                                                   
    
    > sub( "Location (\\d+[EW]) (\\d+[NS])(.+$)", "\\1,\\2", metadata[2])
    [1] "654300E,294600N"
    
    以下是作为角色的结果。在将
    用作.numeric
    之前,您需要进行一些进一步的处理以去除星号。我用一个专栏来说明它。您可能可以使用
    元数据[9]

     widths=c(3,4,4,7,8,7,10,7)
    
     dat=read.fwf( "https://www.metoffice.gov.uk/pub/data/weather/uk/climate/stationdata/lowestoftdata.txt", widths = widths , skip=8, colClasses="character", header=FALSE)
    Warning message:
    In readLines(file, n = thisblock) :
      incomplete final line found on 'https://www.metoffice.gov.uk/pub/data/weather/uk/climate/stationdata/lowestoftdata.txt'
     tail(dat)
    #---------------------
          V1   V2   V3      V4       V5      V6         V7      V8
    1269     2020    9    19.6 *   11.5 *       0*   97.1*   168.6
    1270     2020   10    14.2 *    9.0 *       0*   85.7*    58.8
    1271     2020   11    12.5 *    6.1 *       0*   31.9*    73.7
    1272     2020   12     7.7 *    2.9 *       6*  105.8*    50.5
    1273     2021    1     5.8 *    1.2 *     1 0*   78.6*    49.4
    1274     2021    2     7.9 *    2.4 *       9*   48.6*    84.7
    #----------------
    head(dat)
       V1   V2   V3      V4       V5      V6         V7     V8
    1     1914    1     5.2      0.7     ---      52.0     ---
    2     1914    2     9.2      3.5     ---      28.0     ---
    3     1914    3    ---      ---      ---      ---      ---
    4     1914    4    12.9      5.3     ---      18.0     ---
    5     1914    5    13.7      7.2     ---      38.0     ---
    6     1914    6    16.2     10.4     ---      38.0     ---
    
    summary(as.numeric(sub("[*]","", dat$V8)))
    #--------------------
       Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
       11.0    70.3   136.3   136.1   189.9   314.4     157 
    

    还有
    ?readr::read_fwf
    ,它有一些优点。首先,它允许您使用大于宽度的位置指定fwf。我发现这更容易,尤其是如果您使用我的临时“标尺”。

    下面是一种请求“解析”标题文本的方法:

    metadata <- 
     readLines(url("https://www.metoffice.gov.uk/pub/data/weather/uk/climate/stationdata/lowestoftdata.txt"), n=9)
    > metadata
    [1] "Lowestoft / Lowestoft Monckton Ave from Sept 2007"                                                                                        
    [2] "Location 654300E 294600N 25m amsl to July 2007 "                                                                                          
    [3] "& from Sept 2007 653000E 293800N, Lat 52.483 Lon 1.727, 18m amsl"                                                                         
    [4] "Estimated data is marked with a * after the value."                                                                                       
    [5] "Missing data (more than 2 days missing in month) is marked by  ---."                                                                      
    [6] "Sunshine data taken from an automatic Kipp & Zonen sensor marked with a #, otherwise sunshine data taken from a Campbell Stokes recorder."
    [7] "   yyyy  mm   tmax    tmin      af    rain     sun"                                                                                       
    [8] "              degC    degC    days      mm   hours"  
    
                                                                                   
    
    > sub( "Location (\\d+[EW]) (\\d+[NS])(.+$)", "\\1,\\2", metadata[2])
    [1] "654300E,294600N"
    
    以下是作为角色的结果。在将
    用作.numeric
    之前,您需要进行一些进一步的处理以去除星号。我用一个专栏来说明它。您可能可以使用
    元数据[9]

     widths=c(3,4,4,7,8,7,10,7)
    
     dat=read.fwf( "https://www.metoffice.gov.uk/pub/data/weather/uk/climate/stationdata/lowestoftdata.txt", widths = widths , skip=8, colClasses="character", header=FALSE)
    Warning message:
    In readLines(file, n = thisblock) :
      incomplete final line found on 'https://www.metoffice.gov.uk/pub/data/weather/uk/climate/stationdata/lowestoftdata.txt'
     tail(dat)
    #---------------------
          V1   V2   V3      V4       V5      V6         V7      V8
    1269     2020    9    19.6 *   11.5 *       0*   97.1*   168.6
    1270     2020   10    14.2 *    9.0 *       0*   85.7*    58.8
    1271     2020   11    12.5 *    6.1 *       0*   31.9*    73.7
    1272     2020   12     7.7 *    2.9 *       6*  105.8*    50.5
    1273     2021    1     5.8 *    1.2 *     1 0*   78.6*    49.4
    1274     2021    2     7.9 *    2.4 *       9*   48.6*    84.7
    #----------------
    head(dat)
       V1   V2   V3      V4       V5      V6         V7     V8
    1     1914    1     5.2      0.7     ---      52.0     ---
    2     1914    2     9.2      3.5     ---      28.0     ---
    3     1914    3    ---      ---      ---      ---      ---
    4     1914    4    12.9      5.3     ---      18.0     ---
    5     1914    5    13.7      7.2     ---      38.0     ---
    6     1914    6    16.2     10.4     ---      38.0     ---
    
    summary(as.numeric(sub("[*]","", dat$V8)))
    #--------------------
       Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
       11.0    70.3   136.3   136.1   189.9   314.4     157 
    
    还有
    ?readr::read_fwf
    ,它有一些优点。首先,它允许您使用大于宽度的位置指定fwf。我发现这更容易,尤其是如果你用我的临时“尺子”的话。

    还有一个办法:

    清理这件事需要一大堆不同的东西

    首先,处理两行标题(这总是一件痛苦的事情)。可能有更简单的解决方案,但在某个时候,您只需要完成工作

    我将这两行合并为一行,并使用那些稍长的文本作为标题

    读取数据之前的清理步骤有点神秘,但它会从行尾除去任何不是数字、破折号或星星的东西。(修剪那些文本注释,否则会用fread解析字段,因为fread速度非常快。)

    
    库(数据表)
    图书馆(purrr)
    raw.text这是另一个尝试:

    清理这件事需要一大堆不同的东西

    首先,处理两行标题(这总是一件痛苦的事情)。可能有更简单的解决方案,但在某个时候,您只需要完成工作

    我将这两行合并为一行,并使用那些稍长的文本作为标题

    读取数据之前的清理步骤有点神秘,但它会从行尾除去任何不是数字、破折号或星星的东西。(修剪那些文本注释,否则会用fread解析字段,因为fread速度非常快。)

    
    库(数据表)
    图书馆(purrr)
    
    raw.text这是一种固定宽度的格式。可能使用
    utils::read.fwf
    这是一种固定宽度的格式。也许使用了
    utils::read.fwf
    漂亮的尺子把戏!尺子的把戏很好。我勾选了天狼星的答案,因为这对我来说似乎是最直观的,但这一个也很好,在两者之间,我让它工作得非常出色。谢谢你花时间来帮助我。很好的尺子把戏!尺子的把戏很好。我勾选了天狼星的答案,因为这对我来说似乎是最直观的,但这一个也很好,在两者之间,我让它工作得非常出色。谢谢你花时间来帮助我。工作做得很漂亮,我甚至能理解背后的逻辑。添加
    mutate_all(funs(str_replace(,“[*#],”))
    将数据清理成我需要的内容。谢谢你花时间教我这一点,非常感谢。工作做得很漂亮,我甚至能理解背后的逻辑。添加
    mutate_all(funs(str_replace(,“[*#],”))
    将数据清理成我需要的内容。谢谢你花时间教我这个,非常感谢。
    
          yyyy mm tmax degC tmin degC af days rain mm sun hours
       1: 1914  1       5.2       0.7    <NA>    52.0      <NA>
       2: 1914  2       9.2       3.5    <NA>    28.0      <NA>
       3: 1914  3      <NA>      <NA>    <NA>    <NA>      <NA>
       4: 1914  4      12.9       5.3    <NA>    18.0      <NA>
       5: 1914  5      13.7       7.2    <NA>    38.0      <NA>
      ---                                                      
    1270: 2020 10     14.2*      9.0*      0*   85.7*     58.8*
    1271: 2020 11     12.5*      6.1*      0*   31.9*     73.7*
    1272: 2020 12      7.7*      2.9*      6*  105.8*     50.5*
    1273: 2021  1      5.8*      1.2*     10*   78.6*     49.4*
    1274: 2021  2      7.9*      2.4*      9*   48.6*     84.7*