Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Data.txt:

Index;Time; 1;2345; 2;1423; 3;5123; 

The code:

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

The result:

  X1 X2345 1  2  1423 2  3  5123 

I expect the header to be Index and Time, as follows:

  Index Time 1   2   1423 2   3   5123 

How do I do that?

like image 983
hans-t Avatar asked May 08 '14 13:05

hans-t


People also ask

What does header true mean in R?

• The header = TRUE argument tells R that the first row of your. file contains the variable names.


1 Answers

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) 
like image 78
Beasterfield Avatar answered Sep 21 '22 07:09

Beasterfield