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

后端 未结 5 794
谎友^
谎友^ 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 10:06

    You could (in most cases), sub out the ending ; write a new file without the second row (which is really the first row because of the header), and use read.csv instead of read.table

    > txt <- "Index;Time;
      1;2345;
      2;1423;
      3;5123;" 
    > writeLines(sub(";$", "", readLines(textConnection(txt))[-2]), 'newTxt.txt')
    > read.csv('newTxt.txt', sep = ";")
    ##   Index Time
    ## 1     2 1423
    ## 2     3 5123
    

提交回复
热议问题