【问题标题】:How to read and print first head of a file in R?如何在R中读取和打印文件的第一个头?
【发布时间】:2012-03-21 01:24:16
【问题描述】:

我想在 R 中打印文件的头部。我知道如何使用 read.table 和 R 支持的其他输入方法。我只想知道 R 替代 unix 命令 cat 或 head 读取文件和打印其中一些。

谢谢,

桑楚

【问题讨论】:

  • head()tail(),并查看?[ 以获得更复杂的索引/提取选项。
  • 你显然还不知道 readLines() 和这个函数的 n 参数...

标签: r unix head cat


【解决方案1】:

read.table() 接受 nrows 参数就是为了这个目的:

read.table(header=TRUE, text="
    a b
    1 2
    3 4
    ", nrows=1)
#   a b
# 1 1 2

如果您改为使用readLines() 读入(可能结构较少的)文件,则可以改用其n 参数:

readLines(textConnection("a b
1 2 3 4 some other things
last"), n=1)
# [1] "a b"

【讨论】:

    猜你喜欢
    • 2014-07-05
    • 1970-01-01
    • 2021-09-16
    • 2014-04-12
    • 1970-01-01
    • 1970-01-01
    • 2022-11-03
    • 2020-11-30
    • 2020-12-16
    相关资源
    最近更新 更多