使用read跳过特定的行。csv R中的

时间:2021-09-05 20:08:41

I wish to skip the 1st and 3rd rows of my csv file when importing the file into a data frame in R.

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

In the original file my headers are on line 2.

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

Using the skip argument in read.csv I can skip the 1st line and set the header argument to TRUE by I still have the 3rd line from the original file in my data frame.

在read中使用skip参数。csv,我可以跳过第一行,将标题参数设置为TRUE,因为我的数据框中还有来自原始文件的第三行。

Can anyone suggest how to skip multiple specific rows in R, below is what I was able to cobble together?

有人能建议如何跳过R中的多个特定的行吗?

Can I pass a vector to the skip argument specifying the exact rows to ignore?

我可以将一个向量传递给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)

2 个解决方案

#1


25  

One way to do this is using two read.csv commands, the first one reads the headers and the second one the data:

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

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

I've created the following text file to test this:

我创建了下面的文本文件来测试这个:

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

The result is:

其结果是:

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

#2


0  

My perfect solution:

我的完美的解决方案:

#' 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)
}

#1


25  

One way to do this is using two read.csv commands, the first one reads the headers and the second one the data:

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

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

I've created the following text file to test this:

我创建了下面的文本文件来测试这个:

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

The result is:

其结果是:

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

#2


0  

My perfect solution:

我的完美的解决方案:

#' 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)
}