【问题标题】:How to generate continuous time series data based on pattern matching output in large file list in R?如何根据R中大文件列表中的模式匹配输出生成连续的时间序列数据?
【发布时间】:2018-03-18 23:09:35
【问题描述】:

我已经以 .csv 格式投影数据属性表,其中所有德国气象站都使用 QGIS 投影到相应的德国区级多边形形状文件,该文件可以在 Projected Data 找到。我还清理了德国的历史天气数据(来自 ftp 服务器:Germany weather data),数据即时可用:German historical data on the fly

但是,在Projected Data属性表中,每个气象站具有相同的StationsnameNAME_2属性,但具有不同的station.Identification,因为它包含不同时期的历史天气数据,其各自的天气数据存储在其中在German historical data on the fly。我需要连续 35 年的数据来分析,但每个站的不同时期的历史数据都存储在各自的 .csv 文件中,并带有唯一的 Station.Identification 数字(5 位数字)。

这是投影数据的样子(.csv 数据可以在 fly:Projected Data 获得):

> joinedData[1:10,]
   Stationsname Stations_ID ID__Index Station.Identification   Width Length Station_Height River_Basin Federal_state
1          Aach           1        KL                  00597 47.8410 8.8490            478          NA            BW
2          Aach           1        RR                  08334 47.8410 8.8490            478          NA            BW
3    Aach/Hegau       10771        PE                  02171 47.8500 8.8500            480          NA            BW
4        Aachen           3        EB                  00286 50.7827 6.0941            202      803100            NW
5        Aachen           3        FF                  00286 50.7827 6.0941            202      803100            NW
6        Aachen           3        KL                  00286 50.7827 6.0941            202      803100            NW
7        Aachen           3        MI                  00286 50.7827 6.0941            202      803100            NW
8        Aachen           3        MN                  01901 50.7827 6.0941            202      803100            NW
9        Aachen           3        PE                  05647 50.7827 6.0941            202      803100            NW
10       Aachen           3        RR                  10600 50.7827 6.0941            202      803100            NW
        Begin        End ID_0 ISO  NAME_0 ID_1               NAME_1 ID_2                 NAME_2   HASC_2 CCN_2 CCA_2
1  01.01.1937 30.06.1986   86 DEU Germany    1 Baden-Württemberg   22               Konstanz DE.BW.KN     0  8335
2  01.01.1912 30.06.1986   86 DEU Germany    1 Baden-Württemberg   22               Konstanz DE.BW.KN     0  8335
3                          86 DEU Germany    1 Baden-Württemberg   22               Konstanz DE.BW.KN     0  8335
4  01.01.1951 31.03.2011   86 DEU Germany   10  Nordrhein-Westfalen  290 Städteregion Aachen DE.NW.AC     0  5334
5  01.01.1937 31.03.2011   86 DEU Germany   10  Nordrhein-Westfalen  290 Städteregion Aachen DE.NW.AC     0  5334
6  01.01.1891 31.03.2011   86 DEU Germany   10  Nordrhein-Westfalen  290 Städteregion Aachen DE.NW.AC     0  5334
7  28.04.1993 07.10.2008   86 DEU Germany   10  Nordrhein-Westfalen  290 Städteregion Aachen DE.NW.AC     0  5334
8  01.10.2008 03.04.2012   86 DEU Germany   10  Nordrhein-Westfalen  290 Städteregion Aachen DE.NW.AC     0  5334
9                          86 DEU Germany   10  Nordrhein-Westfalen  290 Städteregion Aachen DE.NW.AC     0  5334
10 01.01.1891 31.03.2011   86 DEU Germany   10  Nordrhein-Westfalen  290 Städteregion Aachen DE.NW.AC     0  5334
      TYPE_2 ENGTYPE_2 NL_NAME_2 VARNAME_2
1  Landkreis  District        NA          
2  Landkreis  District        NA          
3  Landkreis  District        NA          
4      Kreis  District        NA          
5      Kreis  District        NA          
6      Kreis  District        NA          
7      Kreis  District        NA          
8      Kreis  District        NA          
9      Kreis  District        NA          
10     Kreis  District        NA 

这是德国历史天气数据列表的样子(所有1080个txt文件都可以在German historical data on the fly找到):

> files[1:10]
 [1] "produkt_klima_monat_17190101_20161231_00403.txt"
 [2] "produkt_klima_monat_17570301_19611130_01425.txt"
 [3] "produkt_klima_monat_17810101_20161231_02290.txt"
 [4] "produkt_klima_monat_17880101_20161231_05099.txt"
 [5] "produkt_klima_monat_17920101_19840731_04927.txt"
 [6] "produkt_klima_monat_18010101_19531231_03382.txt"
 [7] "produkt_klima_monat_18240101_20161231_02444.txt"
 [8] "produkt_klima_monat_18300101_19621231_00402.txt"
 [9] "produkt_klima_monat_18340101_20161231_03126.txt"
[10] "produkt_klima_monat_18350101_20020331_01891.txt"

为此,我想出了这个可能的管道:

  1. Projected Data中,每个站都有相同的StationsNameNAME_2属性但它有不同的Station.Identification属性,所以我打算根据相同的StationsnameNAME_2的组合来提取每个单独的站),其中查找Station.Identification 属性对于下一步操作很重要)。
  2. 然后基于提取的每个站或行(每行必须有相同的NAME_2,但可能有稍微不同的Stationsname)和Station.Identification 列,所以使用它作为查询来查找5 位数字Station.Identification German historical data on the fly 中的文件列表,其中German historical data on the fly 中的每个文件都以末尾的 5 位数字命名。
  3. 基于这个匹配结果,我打算将这些.txt文件合并到German historical data on the fly中,为每个站点生成连续的时间序列数据,结果可以命名为Stationname+NAME_2.csv`。

我相信这需要一些 R 中的正则表达式处理和模式匹配经验,我很难在 R 中轻松完成这一点,因为在 REGEX 中的经验很少。我需要每个站点的连续数据记录,我必须根据匹配的Station.Identification 合并German historical data on the fly 中的txt 文件,这两个文件都出现在Projected DataGerman historical data on the flytxt 文件的扩展名。如何在 R 中轻松实现这一点?是否有任何解决方法可以更有效地在 R 中完成这些工作?有什么想法吗?谢谢

【问题讨论】:

  • 您对 REGEX 的经验很少。我没有R的经验。因此,要获得一个正则表达式,您必须找一个在 R 正则表达式方面有丰富经验的人。否则,您必须逐字删除 R 并将其转换为随意的术语和大量示例。正则表达式确实是一种单独的语言,而不是真正的其他语言依赖项(与某些人所说的相反)。

标签: r regex dataframe merge pattern-matching


【解决方案1】:

@Dan 这个数据创建示例有点复杂,但这里有一些伪代码想法可以提供帮助。下面的名称更笼统,与您的数据集并不完全相关。包含tidyrdplyrtidyverse 库是解决这些问题的好工具。

首先总是将多个 csv 组合成一个数据框,并将 file_name 添加为一列。函数separate 从一列生成多列。所以像下面这样的东西通过在_上分隔从file_name列创建新列station_idobs_date _

new_df <- df %>%
  separate(file_name, c("station_id", "obs_date"), sep = "_", remove = FALSE)

对于正则表达式或字符串操作,请查看 stringr 库。 mutate 函数创建新列。所以像

new_df <- df %>%
  mutate(observation_date = as.Date(obs_date), new_id = str_extract("\\5d"))

这是一个用于匹配的管道概念

library(tidyverse)

## pseudocode

## for the first set of files build a reference
ref1 <- first_set %>% 
  select(name1, name2, station_id) %>%
  distinct() %>%           # unique combinations
  mutate(new_id = ....)    # add regex or formattinf

## for the second set of files build a reference
ref2 <- second_set %>%
  select(name1, name2, other_station_id) %>%
  distinct() %>%              # unique combinations
  mutate(new__id =  .... )    # add regex or formatting or whatever

## now join the reference to get a cross reference table
## missing entries wil have NA. 
ref <- full_join(ref1, ref2, by = "new_id")

## when your refernce table is looking clean then you can append the new ids
ref1 <- ref1 %>%
  left_join(ref, by = c("name1", "name2", "station_id")) %>%
  select(- ) ## use - to drop values not needed

ref2 <- ref2 %>%
  left_join(ref, by=c("name1", "name2", "other_station_id" )) %>%
  select(- )  ## use - to drop values not neede

现在这两个都有一个 new_id 列,可用于连接或其他匹配

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-16
    • 2015-06-14
    • 1970-01-01
    • 2015-10-13
    • 2019-11-14
    • 1970-01-01
    • 2019-07-11
    • 1970-01-01
    相关资源
    最近更新 更多