ayee
ayee

Reputation: 11

How to read in CSV file that has tab row delimiters and pipe '|' column delimiters?

I am trying to read in a CSV file into R that has row and column delimiters:

A vertical bar or pipe | is the row delimiter. Tab is the column delimiter.

Example dataset:

Column A      Column B      Column C|Red            Shorts            10|Blue            Shirt             7|Yellow          Skirt            15

I'm not sure how to specify both of these in the read.csv function as there is on the option for field separator characters with 'sep'.

Upvotes: 1

Views: 2347

Answers (2)

ayee
ayee

Reputation: 11

Did not seem possible in R so I ended up reading the data into Python, replacing the pipe delimiter with new line and then was able to then read the data in R as it now only contained one delimiter.

Thanks for all the help.

Upvotes: 0

Laksitha Ranasingha
Laksitha Ranasingha

Reputation: 4517

As you said, if the .csv is | seperated. It should look like below. For example; imagine there's a file called foo.csv on with content,

Column A|Column B|Column C
|Red|Shorts|10

And the R code to read it would be;

foo <- read.csv('<path to>/foo.csv', sep = "|")
head(foo)

If the delimiter is a tab, you have to do;

foo <- read.csv('<path to>/foo.csv', sep = "\t")

But not both.

Hope this helps.

Upvotes: 0

Related Questions