0

I want to replace every "€" in a string with "[euro]". Now this works perfectly fine with

    file.col.name <- gsub("€","[euro]", file.col.name, fixed = TRUE)

Now I am looping over column names from a csv-file and suddenly I have trouble with the string "total€". It works for other special character (#,?) but the € sign doesn't get recognized.

    grep("€",file.column.name)

also returns 0 and if I extract the last letter it prints "€" but

    print(lastletter(file.column.name) == "€") 

returns FALSE. (lastletter is just a function to extract the last letter of a string.)

Does anyone have an idea why that happens and maybe an idea to solve it? I checked the class of "file.column.name" and it returns "character", also tried to convert it into a character again and stuff like that but didn't help.

Thank you!

Ed Morton
  • 188,023
  • 17
  • 78
  • 185
mariego
  • 53
  • 1
  • 6
  • Please provide a reproducible example. I can't reproduce with `DF <- iris; names(DF)[1] <- "total€"; grep("€", names(DF))`. Also share your system details, since this might be OS specific. – Roland Sep 08 '16 at 10:48
  • Unfortunately I can't reproduce this problem as well, it only happens with this file. I'm working on windows 10 and using R studio Version 0.99.893 – mariego Sep 08 '16 at 10:56

1 Answers1

1

Your encodings are probably mixed. Check the encodings of the files, then add the appropriate encoding to, e.g., read.csv using fileEncoding="…" as an argument.

If you are working under Unix/Linux, the file utility will tell you the encoding of text files. Otherwise, any editor should show you the encoding of the files.

Common encodings are UTF-8, ISO-8859-15 and windows-1252. Try "UTF-8", "windows-1252" and "latin-9" as values for fileEncoding (the latter being a portable name for ISO-8859-15 according to R's documentation).

Martin Nyolt
  • 4,463
  • 3
  • 28
  • 36
  • Thank you, how can I find out the file encodings? I removed all attributes from the file but this didn't make a change. – mariego Sep 08 '16 at 10:55
  • Have you tried googling? (I don't want to be rude, but detecting file encoding is a different question - and even not really on-topic on Stack Overflow, although [there is a question](http://stackoverflow.com/questions/3710374/get-encoding-of-a-file-in-windows).) – Martin Nyolt Sep 08 '16 at 11:24
  • Sorry, I somehow assumed I had to check it with R. So it was encoded as "ANSI" and I set encoding = "ANSI" in read.csv with no success. Also I tried to save the file in other standards but this made it only worse. Which encoding could I try? – mariego Sep 08 '16 at 11:56
  • Hm I tried all of them and it makes no difference. Also changed the encoding in the file without success. Do you think using the "encoding" option would make sense? Else I will probably try on a different machine to see what's the issue. – mariego Sep 08 '16 at 12:58