How can I read the header but also skip lines - read.table()? How can I read the header but also skip lines - read.table()? r r

How can I read the header but also skip lines - read.table()?


I am afraid, that there is no direct way to achieve this. Either you read the entire table and remove afterwards the lines you don't want or you read in the table twice and assign the header later:

header <- read.table('data.txt', nrows = 1, header = FALSE, sep =';', stringsAsFactors = FALSE)dat    <- read.table('data.txt', skip = 2, header = FALSE, sep =';')colnames( dat ) <- unlist(header)


You're using skip incorrectly. Try this:

dat <- read.table('data.txt', nrows = 2, header =TRUE, sep =';')[-1, ]


The solution using fread from data.table.

require(data.table)fread("Data.txt", drop = "V3")[-1]

Result:

> fread("Data.txt", drop = "V3")[-1]   Index Time1:     2 14232:     3 5123