37

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

In the original file my headers are on line 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.

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

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

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)
TheGoat
  • 2,587
  • 3
  • 25
  • 58
  • I'd be interested to see what others have to say, but if row 1 and 3 are not blank you may need to skip all 3 and manage your header names manually. – Sevyns Aug 23 '16 at 21:35
  • 2
    If you don't mind manually modifying your file, you can a `comment character` like `#` to the 3rd row and then do: `read.csv(file, skip = 1, header = T, comment.char = "#")`. – Abdou Aug 24 '16 at 02:09
  • Why not skip the first row, start reading the csv (incl. header) from row 2, and delete the third row after read.csv()? – mdd Aug 24 '16 at 02:33
  • Related post: https://stackoverflow.com/questions/15860071/read-csv-header-on-first-line-skip-second-line – zx8754 May 15 '19 at 06:01

2 Answers2

73

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

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
R. Schifini
  • 9,085
  • 2
  • 26
  • 32
2

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)
}
Jerry T
  • 1,541
  • 1
  • 19
  • 17
  • This solved a tricky issue for me where I had ascii files with metadata at the bottom of the file that I needed to skip, and a header that I needed to skip. I like that I can also pass commands to read.csv when I use the function. I used: test <- ez.read(paste(path, "filename.ASC", sep = ""), skip.rows = c(233:247), sep = "", header = FALSE, col.names = col_names) – user2860703 Jun 20 '19 at 16:15
  • I am glad it helps! – Jerry T Jun 21 '19 at 18:00