3

I want to import a dataset where numbers are formatted with commas of decimal points, i.e. 0,5 instead of 0.5

Here is the dummy data.

ID|Item|Transaction Type|Country|Product Code|LCY_1|LCY_2|LCY_ 3|Country Code
1801|Furniture|D|Scotland|0|-15,234|-15,14|-15,34|SC
1802|Book|D|Wales|0|-15,234|-15,14|-15,34|WL
1803|Phone|D|UK|0|-21,234|-15,14|-15,34|UK
1804|Earbuds|D|Ireland|0|-19,234|-15,14|-15,34|IR
1805|Mug|D|Ireland|0|-32,234|-18,14|-15,34|SC

Was able to import as follows to the most proper view. However, didn't set "," as decimal separator within read_delim(). Also tried with read_csv() using del = "," but didn't work.

df <- read_delim("df.csv", "|", quote = "\\\"", 
escape_double = FALSE, trim_ws = TRUE)

How can I import that successfully?

kimi
  • 525
  • 5
  • 17
  • If you use the base R `read.delim2("df.csv", sep="|")` You can read this. Is it a requirement to use the `readr` package (which you didn't seem to explicitly mention in your question)? – MrFlick Nov 07 '18 at 20:29
  • 1
    If this is just a readr question, then this is a dup of [How to use “cols()” and “col_double” with respect to comma as decimal mark](https://stackoverflow.com/questions/43234976/how-to-use-cols-and-col-double-with-respect-to-comma-as-decimal-mark) – MrFlick Nov 07 '18 at 20:32
  • Tried read_delim2() but imported all values into one column. read.delim2("df.csv", header = TRUE, sep="|", dec = ",") – kimi Nov 07 '18 at 20:36
  • Did you try `read_delim2()` or `read.delim2()` These are different things – MrFlick Nov 07 '18 at 20:42
  • Sorry, I tried read.delim2() – kimi Nov 07 '18 at 20:43
  • 5
    That happened with the above file? I can't replicate. It reads fine for me. But as indicated in the duplicate i provided, if you want to use `read_delim`, use `read_delim("df.csv", "|", locale=locale(decimal_mark = ","))` – MrFlick Nov 07 '18 at 20:45
  • Exactly. Now works after locale. Haven't used locale feature in readr(). Cheers – kimi Nov 07 '18 at 20:53

0 Answers0