gpt4 book ai didi

r - 在 R 的 read.table() 中指定多字符注释标记

转载 作者:行者123 更新时间:2023-12-02 09:39:10 24 4
gpt4 key购买 nike

是否可以在 R 中指定由 1 个以上符号组成的注释字符?

例如,

read.table("data.dat", comment.char="//") 

行不通。

最佳答案

我认为你不能,但这里有一个解决方法。读取文件的函数,使用 sub 清理其行,并在将其传递给 read.table 之前将所有内容粘贴回一起:

my.read.table <- function(file, comment.char = "//", ...) {
clean.lines <- sub(paste0(comment.char, ".*"), "", readLines(file))
read.table(..., text = paste(clean.lines, collapse = "\n"))
}

测试:

file <- textConnection("3 4 //a
1 2")
my.read.table(file)
# V1 V2
# 1 3 4
# 2 1 2

关于r - 在 R 的 read.table() 中指定多字符注释标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15513956/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com