读取带有不规则标题的文本文件(在 R 中)

发布于 2024-12-27 17:41:44 字数 328 浏览 1 评论 0原文

我正在尝试将一个平面文件读入 R。

它由 ';' 分隔并有 12 行注释来描述内容。 我想阅读该文件并排除注释。

但问题是注释行 11 包含数据标头,如下所示:

# Fields: labno;姓名;多布;性别;地点; date

有没有一种方法可以从注释中提取标题并将其应用到数据中。我的想法是只读取前 11 行,并将 labno 中的所有内容存储为向量。我将从第 13 行读取所有内容,并使用存储向量作为日期的列名称。

有没有办法读取前 11 行并删除 labno 之前的所有内容,

谢谢。

I am trying to read a flat file into R.

It is separated by ';' and has 12 leading lines of comments to describe the content.
I want to read the file and exlude the comments.

The problem however is that the commented line 11 contains the data headers as follows:

# Fields: labno; name; dob; sex; location; date

Is there a way that I can extract the headers form the comments and apply them to the data. The way I thought of doing it was to read the first 11 lines only and store everything from labno as a vector. The I would read everything from line 13 and use the store vector as column names for the the date.

Is there a way to read the first 11 lines and remove everything before labno

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半岛未凉 2025-01-03 17:41:44

步骤 1:(仅读取包含列名称的第十一行。)

hdrs <- read.table("somefile.txt", nrows=1, skip=10, comment.char="")

步骤 2:(读取文件的其余部分,允许默认自动名称)

dat <- read.table("somefile.txt", skip=12)

步骤 3:(在应用“字段”作为列名称之前删除无关字符)

names(dat)  <- scan(textConnection(sub("# Fields\\:", "", hdrs)), 
                      what="character", sep=";")

更高版本的 R 允许 ' scan' 以获得一个 'text' 参数,而不是需要尴尬的 textConnection 函数。

Step1: (read only the eleventh row containing column names. )

hdrs <- read.table("somefile.txt", nrows=1, skip=10, comment.char="")

Step2: (read the rest of the file, allowing default automatic names)

dat <- read.table("somefile.txt", skip=12)

Step3: (remove extraneous characters before applying the ‘fields’ as column names)

names(dat)  <- scan(textConnection(sub("# Fields\\:", "", hdrs)), 
                      what="character", sep=";")

Later versions of R allow ‘scan’ to have a ‘text’ argument rather than requiring the awkward textConnection function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文