Z. Zhang
Z. Zhang

Reputation: 501

R - have trouble reading CSV file

I need to analyze data in .csv files but those files have a problem that the second row is always dashes. Please see example below:

participant_id  participant_name    N371    N2062

--------------  ----------------    ----    -----

229182         John Kim  0   0

read.csv or fread is not able to read this file correctly until I open these files and manually delete the second row. However, it is a pain and those files will be updated weekly. I am wondering if there is a way that I can read these files correctly.

Thanks.

Upvotes: 0

Views: 585

Answers (1)

Zelazny7
Zelazny7

Reputation: 40618

In this particular case you can use the comment.char argument:

text <- "participant_id participant_name N371 N2062
-------------- ---------------- --- ----
  229182 'John Kim' 0 0" 

df <- read.table(text = text, header=T, comment.char = "-")

> df
  participant_id participant_name N371 N2062
1         229182         John Kim    0     0

Upvotes: 2

Related Questions