在 R 中使用 read.csv 跳过特定行

Skip specific rows using read.csv in R

我希望在将文件导入到 R 中的数据框中时跳过我的 csv 文件的第一行和第三行。

在原始文件中,我的 header 在第 2 行。

使用 read.csv 中的 skip 参数,我可以跳过第一行并将 header 参数设置为 TRUE,因为我的数据框中仍然有原始文件的第三行。

任何人都可以建议如何跳过 R 中的多个特定行,下面是我拼凑的内容?

我能否将向量传递给 skip 参数以指定要忽略的确切行?

prach <- read.csv("RSRAN104_-_PRACH_Propagation_Delay-PLMN-day-rsran_RU50EP1_reports_RSRAN104_xml-2016_08_23-21_33_03__604.csv", header = TRUE, sep = ",", stringsAsFactors = FALSE, skip = 1)

一种方法是使用两个 read.csv 命令,第一个读取 headers,第二个读取数据:

headers = read.csv(file, skip = 1, header = F, nrows = 1, as.is = T)
df = read.csv(file, skip = 3, header = F)
colnames(df)= headers

我创建了以下文本文件来对此进行测试:

do not read
a,b,c
previous line are headers
1,2,3
4,5,6

结果是:

> df
  a b c
1 1 2 3
2 4 5 6

我的完美解决方案:

#' read csv table, wrapper of \code{\link{read.csv}}
#' @description read csv table, wrapper of \code{\link{read.csv}}
#' @param tolower whether to convert all column names to lower case
#' @param skip.rows rows to skip (1 based) before read in, eg 1:3
#' @return returns a data frame
#' @export
ez.read = function(file, ..., skip.rows=NULL, tolower=FALSE){
    if (!is.null(skip.rows)) {
        tmp = readLines(file)
        tmp = tmp[-(skip.rows)]
        tmpFile = tempfile()
        on.exit(unlink(tmpFile))
        writeLines(tmp,tmpFile)
        file = tmpFile
    }
    result = read.csv(file, ...)
    if (tolower) names(result) = tolower(names(result))
    return(result)
}