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

后端 未结 5 768
谎友^
谎友^ 2020-12-01 09:17

Data.txt:

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

The code:

dat <- read.table(\'data.txt\', skip = 1, nrows = 2, header =TRU         


        
5条回答
  •  南方客
    南方客 (楼主)
    2020-12-01 09:54

    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)
    

提交回复
热议问题