r - read.csv, header on first line, skip second line -


i have csv file 2 header rows, first row want header, second row want discard. if following command:

data <- read.csv("hk stocks bbg.csv", header = t, stringsasfactors = false) 

the first row becomes header , second row of file becomes first row of data frame:

  xaaaaaaaaa       x x.1     xbbbbbbbbbb     x.2 x.3 1         date px_last  na         date px_last  na 2   31/12/2002  38.855  na   31/12/2002  19.547  na 3   02/01/2003  38.664  na   02/01/2003  19.547  na 4   03/01/2003  40.386  na   03/01/2003  19.547  na 5   06/01/2003  40.386  na   06/01/2003  19.609  na 6   07/01/2003  40.195  na   07/01/2003  19.609  na 

i want skip second row of csv file , get

  x1.hk.equity       x x.1 x2.hk.equity     x.2 x.3 2   31/12/2002  38.855  na   31/12/2002  19.547  na 3   02/01/2003  38.664  na   02/01/2003  19.547  na 4   03/01/2003  40.386  na   03/01/2003  19.547  na 5   06/01/2003  40.386  na   06/01/2003  19.609  na 6   07/01/2003  40.195  na   07/01/2003  19.609  na 

i tried data <- read.csv("hk stocks bbg.csv", header = t, stringsasfactors = false, skip = 1) returns:

        date px_last  x     date.1 px_last.1 x.1 1 31/12/2002  38.855 na 31/12/2002    19.547  na 2 02/01/2003  38.664 na 02/01/2003    19.547  na 3 03/01/2003  40.386 na 03/01/2003    19.547  na 4 06/01/2003  40.386 na 06/01/2003    19.609  na 5 07/01/2003  40.195 na 07/01/2003    19.609  na 6 08/01/2003  40.386 na 08/01/2003    19.547  na 

the header row comes second line of csv file, not first line.

thank you.

this should trick:

all_content = readlines("file.csv") skip_second = all_content[-2] dat = read.csv(textconnection(skip_second), header = true, stringsasfactors = false) 

the first step using readlines reads entire file list, each item in list represents line in file. next, discard second line using fact negative indexing in r means select index. finally, feed data read.csv process data.frame.


Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -