mariego
mariego

Reputation: 53

problems replacing €-symbol in strings

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!

Upvotes: 0

Views: 1069

Answers (1)

Martin Nyolt
Martin Nyolt

Reputation: 4650

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).

Upvotes: 1

Related Questions