【发布时间】:2021-06-27 22:49:26
【问题描述】:
我正在尝试使用 R 从 historical Met Office data 中提取数据表以及其他一些信息,但尽管在 StackOverflow 上度过了整个晚上,但仍然遇到问题。
例如,这是sunny (maybe??) Lowestoft 的数据:
Lowestoft / Lowestoft Monckton Ave from Sept 2007
Location 654300E 294600N 25m amsl to July 2007
& from Sept 2007 653000E 293800N, Lat 52.483 Lon 1.727, 18m amsl
Estimated data is marked with a * after the value.
Missing data (more than 2 days missing in month) is marked by ---.
Sunshine data taken from an automatic Kipp & Zonen sensor marked with a #, otherwise sunshine data taken from a Campbell Stokes recorder.
yyyy mm tmax tmin af rain sun
degC degC days mm hours
1914 1 5.2 0.7 --- 52.0 ---
1914 2 9.2 3.5 --- 28.0 ---
1914 3 --- --- --- --- ---
1914 4 12.9 5.3 --- 18.0 ---
...
2020 11 12.5* 6.1* 0* 31.9* 73.7* Provisional
2020 12 7.7* 2.9* 6* 105.8* 50.5* Provisional
2021 1 5.8* 1.2* 10* 78.6* 49.4* Provisional
2021 2 7.9* 2.4* 9* 48.6* 84.7* Provisional
到目前为止,我管理的最好方法是使用 sed(在 R 之外)删除 *'d 和 #'d 变量,但是使用 read.table(lowestoftdata.text, skip = 8, col.names = c("year","month","max_temp", "min_temp", "frost", "rainfall", "sunshine")) 导入它时遇到 2020 年的数据会失败之后标记为临时。提取纬度和经度值也非常方便,这些值通常在第 2 行,但如果像 Lowestoft 一样,车站在某个点移动,但我的 very 正则表达式有限,则可以在第 3 行知识(和移动的目标)让我失望了。
我的伪代码方法是:
- 用纬度和经度识别线,解析该线以提取这些变量
- 识别以数字开头的第一行,并从该行读取.table
...但是,由于我在处理格式良好的 CSV 文件以外的任何内容方面的有限经验,将其转化为实践证明是具有挑战性的,因此任何关于从哪里开始的建议都将不胜感激。
【问题讨论】:
-
这是一种固定宽度的格式。也许使用
utils::read.fwf
标签: r regex text text-extraction data-extraction