How to Import a CSV file containing multiple sections into R?

Posted by PaulHurleyuk on Stack Overflow See other posts from Stack Overflow or by PaulHurleyuk
Published on 2010-04-13T10:14:35Z Indexed on 2010/04/13 12:02 UTC
Read the original article Hit count: 318

Filed under:
|
|
|

I want to import the contents of a csv file into R, the csv file contains multiple sections of data vertically, seperated by blank lines and asterisks. For example

********************************************************
* SAMPLE DATA ******************************************
********************************************************
Name, DOB, Sex
Rod, 1/1/1970, M
Jane, 5/7/1980, F
Freddy, 9.12,1965, M

*******************************************************
*  Income Data ****************************************
*******************************************************
Name, Income
Rod, 10000
Jane, 15000
Freddy, 7500

I would like to import this into R as two seperate dataframes. Currently I'm manually cutting the csv file up into smaller files, but I think I could do it using read.csv and the skip and nrows settings of read.csv, If I could work out where the secion breaks are.

This gives me a logical TRUE for every blank line

ifelse(readLines("DATA.csv")=="",TRUE,FALSE)

I'm hoping someone has already solved this problem.

© Stack Overflow or respective owner

Related posts about r

    Related posts about csv