跳过行直到具有特定值的行 [英] Skipping rows until row with a certain value

查看:64
本文介绍了跳过行直到具有特定值的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从URL读取.txt文件,但想跳过这些行,直到具有特定值的行为止。网址为 https://fred.stlouisfed.org/data/HNOMFAQ027S.txt 并且数据采用以下格式:

I need to to read a .txt file from an URL, but would like to skip the rows until a row with a certain value. The URL is https://fred.stlouisfed.org/data/HNOMFAQ027S.txt and the data takes the following form:

...(行数)

...(行数)

...(行数)

日期值

1945-01-01 144855

1945-01-01 144855

1946-01- 01 138515

1946-01-01 138515

1947-01-01 136405

1947-01-01 136405

1948-01-01 135486

1948-01-01 135486

1949-01-01 142455

1949-01-01 142455

我想跳过所有行,直到带有 DATE // VALUE的行,然后开始从此行开始导入数据(包括 DATE // VALUE)。有没有办法使用data.table的fread()-或任何其他方式,例如dplyr?

I would like to skip all rows until the row with "DATE // VALUE" and start importing the data from this line onwards (including "DATE // VALUE"). Is there a way to do this with data.table's fread() - or any other way, such as with dplyr?

非常感谢您的努力,你的时间!

Thank you very much in advance for your effort and your time!

最好,

c。

推荐答案

将fread与unix工具结合在一起:

Combining fread with unix tools:

> fread("curl -s https://fred.stlouisfed.org/data/HNOMFAQ027S.txt | sed -n -e '/^DATE.*VALUE/,$p'")
           DATE   VALUE
  1: 1945-10-01    1245
  2: 1946-01-01       .
  3: 1946-04-01       .
  4: 1946-07-01       .
  5: 1946-10-01    1298
 ---                   
282: 2016-01-01 6566888
283: 2016-04-01 6741075
284: 2016-07-01 7022321
285: 2016-10-01 6998898
286: 2017-01-01 7448792
> 

这篇关于跳过行直到具有特定值的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆